Rename almost all occurences of LinuxBIOS to coreboot.
[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 nrv2b
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 < 0xa0000) && ((dest+olen)>0xa0000)) {
84                 dest = (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         rom_end = dest + olen - 1;
101         rom = dest;
102 #else
103         rom = rom_start;
104 #endif
105
106         return 0;
107 }
108
109
110 void stream_fini(void)
111 {
112         return;
113 }
114
115 byte_offset_t stream_skip(byte_offset_t count)
116 {
117         byte_offset_t bytes;
118         bytes = count;
119         if ((rom + bytes - 1) > rom_end) {
120                 printk_warning("%6d:%s() - overflowed source buffer\n",
121                         __LINE__, __FUNCTION__);
122                 bytes = 0;
123                 if (rom <= rom_end) {
124                         bytes = (rom_end - rom) + 1;
125                 }
126         }
127         rom += bytes;
128         return bytes;
129 }
130
131 byte_offset_t stream_read(void *vdest, byte_offset_t count)
132 {
133         unsigned char *dest = vdest;
134         const unsigned char *src = rom;
135         byte_offset_t bytes;
136
137         bytes = stream_skip(count);
138         memcpy(dest, src, bytes);
139         return bytes;
140 }