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