ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / boot / openfirmware / common.c
1 /*
2  * Copyright (C) Paul Mackerras 1997.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include "zlib.h"
11 #include "nonstdio.h"
12 #include "of1275.h"
13 #include <linux/string.h>
14 #include <asm/bootinfo.h>
15 #include <asm/page.h>
16
17 /* Information from the linker */
18 extern char __sysmap_begin, __sysmap_end;
19
20 extern int strcmp(const char *s1, const char *s2);
21 extern char *avail_ram, *avail_high;
22 extern char *end_avail;
23
24 unsigned int heap_use, heap_max;
25
26 struct memchunk {
27     unsigned int size;
28     struct memchunk *next;
29 };
30
31 static struct memchunk *freechunks;
32
33 static void *zalloc(void *x, unsigned items, unsigned size)
34 {
35     void *p;
36     struct memchunk **mpp, *mp;
37
38     size *= items;
39     size = (size + 7) & -8;
40     heap_use += size;
41     if (heap_use > heap_max)
42         heap_max = heap_use;
43     for (mpp = &freechunks; (mp = *mpp) != 0; mpp = &mp->next) {
44         if (mp->size == size) {
45             *mpp = mp->next;
46             return mp;
47         }
48     }
49     p = avail_ram;
50     avail_ram += size;
51     if (avail_ram > avail_high)
52         avail_high = avail_ram;
53     if (avail_ram > end_avail) {
54         printf("oops... out of memory\n\r");
55         pause();
56     }
57     return p;
58 }
59
60 static void zfree(void *x, void *addr, unsigned nb)
61 {
62     struct memchunk *mp = addr;
63
64     nb = (nb + 7) & -8;
65     heap_use -= nb;
66     if (avail_ram == addr + nb) {
67         avail_ram = addr;
68         return;
69     }
70     mp->size = nb;
71     mp->next = freechunks;
72     freechunks = mp;
73 }
74
75 #define HEAD_CRC        2
76 #define EXTRA_FIELD     4
77 #define ORIG_NAME       8
78 #define COMMENT         0x10
79 #define RESERVED        0xe0
80
81 #define DEFLATED        8
82
83 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
84 {
85     z_stream s;
86     int r, i, flags;
87
88     /* skip header */
89     i = 10;
90     flags = src[3];
91     if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
92         printf("bad gzipped data\n\r");
93         exit();
94     }
95     if ((flags & EXTRA_FIELD) != 0)
96         i = 12 + src[10] + (src[11] << 8);
97     if ((flags & ORIG_NAME) != 0)
98         while (src[i++] != 0)
99             ;
100     if ((flags & COMMENT) != 0)
101         while (src[i++] != 0)
102             ;
103     if ((flags & HEAD_CRC) != 0)
104         i += 2;
105     if (i >= *lenp) {
106         printf("gunzip: ran out of data in header\n\r");
107         exit();
108     }
109
110     s.zalloc = zalloc;
111     s.zfree = zfree;
112     r = inflateInit2(&s, -MAX_WBITS);
113     if (r != Z_OK) {
114         printf("inflateInit2 returned %d\n\r", r);
115         exit();
116     }
117     s.next_in = src + i;
118     s.avail_in = *lenp - i;
119     s.next_out = dst;
120     s.avail_out = dstlen;
121     r = inflate(&s, Z_FINISH);
122     if (r != Z_OK && r != Z_STREAM_END) {
123         printf("inflate returned %d msg: %s\n\r", r, s.msg);
124         exit();
125     }
126     *lenp = s.next_out - (unsigned char *) dst;
127     inflateEnd(&s);
128 }
129
130 /* Make a bi_rec in OF.  We need to be passed a name for BI_BOOTLOADER_ID,
131  * a machine type for BI_MACHTYPE, and the location where the end of the
132  * bootloader is (PROG_START + PROG_SIZE)
133  */
134 void make_bi_recs(unsigned long addr, char *name, unsigned int mach,
135                 unsigned long progend)
136 {
137         unsigned long sysmap_size;
138         struct bi_record *rec;
139
140         /* Figure out the size of a possible System.map we're going to
141          * pass along.
142          * */
143         sysmap_size = (unsigned long)(&__sysmap_end) -
144                 (unsigned long)(&__sysmap_begin);
145
146         /* leave a 1MB gap then align to the next 1MB boundary */
147         addr = _ALIGN(addr+ (1<<20) - 1, (1<<20));
148         /* oldworld machine seem very unhappy about this. -- Tom */
149         if (addr >= progend)
150                 claim(addr, 0x1000, 0);
151
152         rec = (struct bi_record *)addr;
153         rec->tag = BI_FIRST;
154         rec->size = sizeof(struct bi_record);
155         rec = (struct bi_record *)((unsigned long)rec + rec->size);
156
157         rec->tag = BI_BOOTLOADER_ID;
158         sprintf( (char *)rec->data, name);
159         rec->size = sizeof(struct bi_record) + strlen(name) + 1;
160         rec = (struct bi_record *)((unsigned long)rec + rec->size);
161
162         rec->tag = BI_MACHTYPE;
163         rec->data[0] = mach;
164         rec->data[1] = 1;
165         rec->size = sizeof(struct bi_record) + 2 * sizeof(unsigned long);
166         rec = (struct bi_record *)((unsigned long)rec + rec->size);
167
168         if (sysmap_size) {
169                 rec->tag = BI_SYSMAP;
170                 rec->data[0] = (unsigned long)(&__sysmap_begin);
171                 rec->data[1] = sysmap_size;
172                 rec->size = sizeof(struct bi_record) + 2 *
173                         sizeof(unsigned long);
174                 rec = (struct bi_record *)((unsigned long)rec + rec->size);
175         }
176
177         rec->tag = BI_LAST;
178         rec->size = sizeof(struct bi_record);
179         rec = (struct bi_record *)((unsigned long)rec + rec->size);
180 }