This patch converts __FUNCTION__ to __func__, since __func__ is standard.
[coreboot.git] / src / stream / rom_stream.c
1 #include <console/console.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <stream/read_bytes.h>
5 #include <string.h>
6
7 /* if they set the precompressed rom stream, they better have set a type */
8 #if CONFIG_PRECOMPRESSED_PAYLOAD && ((!CONFIG_COMPRESSED_PAYLOAD_NRV2B) && (!CONFIG_COMPRESSED_PAYLOAD_LZMA))
9 #error "You set CONFIG_PRECOMPRESSED_PAYLOAD but need to set CONFIG_COMPRESSED_PAYLOAD_NRV2B or CONFIG_COMPRESSED_PAYLOAD_LZMA"
10 #endif
11
12 /* If they set ANY of these, then we're compressed */
13 #if ((CONFIG_COMPRESSED_PAYLOAD_NRV2B) || (CONFIG_COMPRESSED_PAYLOAD_LZMA))
14 #define UNCOMPRESSER 1
15 extern unsigned char _heap, _eheap;
16 #endif
17
18 #if (CONFIG_COMPRESSED_PAYLOAD_NRV2B) 
19 #define HAVE_UNCOMPRESSER 1
20 // include generic nrv2b
21 #include "../lib/nrv2b.c"
22 #endif
23
24 #if (CONFIG_COMPRESSED_PAYLOAD_LZMA)
25 #if HAVE_UNCOMPRESSER
26 #error "You're defining more than one compression type, which is not allowed (of course)"
27 #endif
28 #define HAVE_UNCOMPRESSER 1
29 // include generic lzma
30 #include "../lib/lzma.c"
31 #endif
32
33 #ifndef CONFIG_ROM_PAYLOAD_START
34 #define CONFIG_ROM_PAYLOAD_START 0xffff0000UL
35 #endif
36
37 /* well, this is a mess, and it will get fixed, but not right away.
38  * until we stop using 'ld' for building the rom image, that is.
39  * problem is, that on the sc520, ROM_PAYLOAD_START has to be at 0x2000000.
40  * but if you set CONFIG_ROM_PAYLOAD_START to that, then ld will try to
41  * build a giant image: 0x0-0x2000000, i.e. almost 4 GB.
42  * so make this non-static, non-const for now.
43  */
44
45 /*XXXXXXXXXXXXXX */
46 /*static const */unsigned char *rom_start = (unsigned char *)CONFIG_ROM_PAYLOAD_START;
47 /*static const */unsigned char *rom_end   = (unsigned char *)(CONFIG_ROM_PAYLOAD_START + PAYLOAD_SIZE - 1);
48 /*XXXXXXXXXXXXXX */
49
50 static const unsigned char *rom;
51
52 #if UNCOMPRESSER
53 unsigned long 
54 uncompress(uint8_t * rom_start, uint8_t *dest )
55 {
56 #if (CONFIG_COMPRESSED_PAYLOAD_NRV2B) 
57         unsigned long ilen; // used compressed stream length
58         return unrv2b(rom_start, dest, &ilen);
59 #endif
60 #if (CONFIG_COMPRESSED_PAYLOAD_LZMA)
61         return ulzma(rom_start, dest);
62 #endif
63 }
64 #endif
65 int stream_init(void)
66 {
67 #if (UNCOMPRESSER)
68         unsigned char *dest;
69         unsigned long olen;
70 #endif
71
72         printk_debug("rom_stream: 0x%08lx - 0x%08lx\n",
73                 (unsigned long)rom_start,
74                 (unsigned long)rom_end);
75
76 #if (UNCOMPRESSER) 
77
78         dest = &_eheap; /* need a good address on RAM */
79
80 #if _RAMBASE<0x00100000
81         olen = *(unsigned int *)dest;
82 #if (CONFIG_CONSOLE_VGA==1) || (CONFIG_PCI_ROM_RUN == 1)
83         if((dest < (unsigned char *)0xa0000) && ((dest+olen)>(unsigned char *)0xa0000)) {
84                 dest = (unsigned char *)(CONFIG_LB_MEM_TOPK<<10);
85         }
86 #endif
87         if((dest < (unsigned char *) 0xf0000) && ((dest+olen)> (unsigned char *)0xf0000)) { // coreboot tables etc
88           dest = (unsigned char *) (CONFIG_LB_MEM_TOPK<<10);
89         }
90 #endif
91
92         /* ALL of those settings are too smart and also unsafe. Set the dest to 16 MB: 
93          * known to be safe for LB for now, and mostly safe for all elf images we have tried. 
94          * long term, this has got to be fixed. 
95          */
96         dest  = (unsigned char *) (16 * 1024 * 1024);
97         printk_debug("Uncompressing to RAM 0x%08lx ", dest);
98         olen = uncompress((uint8_t *) rom_start, (uint8_t *)dest );
99         printk_debug(" olen = 0x%08lx done.\n", olen);
100         if (olen != 0) {
101                 rom_end = dest + olen - 1;
102                 rom = dest;
103         } else {
104                 /* Decompression failed, assume payload is uncompressed */
105                 printk_debug("Decompression failed. Assuming payload is uncompressed...\n");
106                 rom = rom_start;
107         }
108 #else
109         rom = rom_start;
110 #endif
111
112         return 0;
113 }
114
115
116 void stream_fini(void)
117 {
118         return;
119 }
120
121 byte_offset_t stream_skip(byte_offset_t count)
122 {
123         byte_offset_t bytes;
124         bytes = count;
125         if ((rom + bytes - 1) > rom_end) {
126                 printk_warning("%6d:%s() - overflowed source buffer\n",
127                         __LINE__, __func__);
128                 bytes = 0;
129                 if (rom <= rom_end) {
130                         bytes = (rom_end - rom) + 1;
131                 }
132         }
133         rom += bytes;
134         return bytes;
135 }
136
137 byte_offset_t stream_read(void *vdest, byte_offset_t count)
138 {
139         unsigned char *dest = vdest;
140         const unsigned char *src = rom;
141         byte_offset_t bytes;
142
143         bytes = stream_skip(count);
144         memcpy(dest, src, bytes);
145         return bytes;
146 }