wok view linld/stuff/src/LOAD.CPP @ rev 20751

Add libsbc
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 12 12:12:36 2019 +0100 (17 months ago)
parents 7f39f4ab7511
children 094f58ac8183
line source
1 // This file is distributed under GPL
3 #include "crtl.h"
4 #include "common.h"
6 /***************
7 Memory layout assumed by kernel boot process
8 --------------------------------------------
9 Note: claims that kernel setup is relocatable are
10 still not 100% valid:
11 bzImage decompressing trashes 10000-8ffff range,
12 so rm loader -> pm kernel info is lost if it was here...
13 So I had to stick to 90000.
15 10000000+------------------------+ <- 256m
16 | initrd | initrd is at top of mem, but
17 | | not higher than 256m
18 +------------------------+
19 +------------------------+
20 | bzImage | bzImage is at 1m
21 | | VCPI/XMS/64k offset tricks used...
22 00100000+------------------------+ <- 1m
23 | video, BIOS etc | Do not use.
24 000A0000+------------------------+
25 | Reserved for BIOS | Do not use. Reserved for BIOS EBDA.
26 0009A000+------------------------+ <- stack top for kernel rm code
27 | Cmdline |
28 00098000+------------------------+ <- heap top for kernel rm code
29 | Kernel setup | The kernel real-mode code.
30 00090200+------------------------+
31 | Kernel boot sector | The kernel legacy boot sector.
32 00090000+------------------------+
33 | Zapped by ungzip | Historically zImages were loaded here
34 | (zImage once was here) | bzImages use this space for ungzipping
35 00010000+------------------------+
36 | Boot loader | <- Boot sector entry point 0000:7C00
37 00001000+------------------------+
38 | Reserved for MBR/BIOS |
39 00000800+------------------------+
40 | Typically used by MBR |
41 00000600+------------------------+
42 | BIOS use only |
43 00000000+------------------------+
44 */
46 struct kernelparams_t {
47 s8 setup_sects; // 01F1 The size of the setup in sectors
48 // boot sector is NOT included here
49 u16 ro_flag; // 01F2 If set, the root is mounted readonly
50 u16 syssize; // 01F4 DO NOT USE - for bootsect.S use only:
51 // size of pm part of kernel
52 // (in 16 byte units, rounded up)
53 u16 swap_dev; // 01F6 DO NOT USE - obsolete
54 u16 ram_size; // 01F8 DO NOT USE - for bootsect.S use only:
55 // if nonzero then kernel
56 // (driver/block/ramdisk.c: rd_load())
57 // will try to load the contents for the ramdisk
58 // from the "root_dev" which MUST then have the
59 // floppyMAJOR
60 // The file-system on that floppy must be MINIX
61 // If rd_load() succeeds it sets the root_dev
62 // to the ramdisk for mounting it
63 u16 vid_mode; // 01FA Video mode control
64 u16 root_dev; // 01FC Default root device number
65 u16 boot_flag; // 01FE 0xAA55 magic number
66 u16 jump; // 0200 Jump instruction
67 u32 header; // 0202 Magic signature "HdrS"
68 u16 version; // 0206 Boot protocol version supported
69 u16 realmode_switch_ofs; // 0208 Hook called just before rm->pm
70 u16 realmode_switch_seg;
71 u16 start_sys_seg; // 020E
72 u16 kernel_version; // 020C Points to kernel version string
73 u8 type_of_loader; // 0210 Boot loader identifier
74 u8 loadflags; // 0211 Boot protocol option flags
75 u16 setup_move_size;// 0212 Move to high memory size (used with hooks)
76 u32 code32_start; // 0214 Boot loader hook (see below)
77 u32 initrd_buf; // 0218 initrd load address (set by boot loader)
78 u32 initrd_size; // 021C initrd size (set by boot loader)
79 u32 bootsect_kludge;// 0220 DO NOT USE - for bootsect.S use only
80 u16 heap_end_ptr; // 0224 Free memory after setup end
81 u16 pad1; // 0226 Unused
82 u32 cmd_line_ptr; // 0228 32-bit pointer to the kernel command line
83 u8 pad30[0x400-0x22c]; // 022C
84 // 02D0 up to 32 20-byte mem info structs from
85 // int 0x15 fn 0xe820
86 }; //__attribute((packed));
88 struct first1k_t {
89 // these two set by rm setup:
90 u16 curr_curs; // 0000 saved cursor position
91 u16 ext_mem_size; // 0002 extended memory size in Kb (from int 0x15 fn 0x88)
92 u8 pad00[0x20-4];
93 // old-style cmdline (not used in LINLD (yet?))
94 u32 cl_magic_ofs;
95 //u16 cl_magic; // 0020 commandline magic number (=0xA33F)
96 //u16 cl_ofs; // 0022 commandline offset
97 u8 pad10[0x80-0x24];
98 // these two set by rm setup:
99 u8 hd0_disk_par[16]; // 0080 hd0-disk-parameter from intvector 0x41
100 u8 hd1_disk_par[16]; // 0090 hd1-disk-parameter from intvector 0x46
101 u8 pad20[0x01e0-0xa0];
102 // this is set by rm setup:
103 u32 alt_mem_size; // 01E0 extended memory size in Kb (from int 0x15 fn 0xe801)
104 u8 pad28[0x01f1-0x1e4];
105 struct kernelparams_t params;
106 }; //__attribute((packed));
108 #if sizeof(first1k_t)!=0x400
109 #error BUG: Bad first1k
110 #endif
112 const u32 HdrS = 'H' + ('d'<<8) + (u32('r')<<16) + (u32('S')<<24);
114 // register value to launch the kernel real mode code
115 static u32 csip;
116 #ifdef NO386
117 extern "C" u16 topseg();
118 #else
119 #define topseg() 0x9000
120 #endif
121 u16 rm_size;
122 static u8 pm_high; // @ = @rm_size + 2, see JUMP.ASM
123 static u8* rm_buf; // @ = @rm_size + 3, see JUMP.ASM
124 struct image_himem imgs[2];
126 u32 base_himem = _1m+_64k;
128 static const char kernel_file_error[] = "Can't use kernel file";
129 char* load_kernel() {
131 struct image_himem *m = &pm;
132 #define _rm_buf(m) (((u8**)(m))[-1])
133 #define _pm_high (((u8*)(m))[-3])
134 #define _rm_size (*(u16*)((u8*)(m)-5))
135 #define _csip (*(u32*)((u8*)(m)-9))
136 char *version_string;
137 {
138 struct kernelparams_t *kernelparams;
139 kernelparams = &(((first1k_t*) (_rm_buf(m) = (u8 *)malloc_or_die(_32k)))->params);
140 #define first1k ((first1k_t*)((u8 *)kernelparams-0x1F1))
142 *((u16 *)&_csip+1)=topseg()|0x20;
143 // Open kernel, read first kb, check it
144 m->errmsg = kernel_file_error;
145 open_image(kernel_name, m);
147 do {
148 // Do not use malloc below until heap_top adjustment (see <*>)
149 if (readrm(m, 0x200) == 0x200) {
151 lseekcur(m->fd,-0x200);
152 if(kernelparams->setup_sects == 0) {
153 #if 1
154 if(* (int *) &first1k->pad10[0x3F-0x24] == 0x3AE8) {
155 _csip+=0xFFE00042;
156 }
157 else
158 #endif
159 kernelparams->setup_sects=4;
160 }
161 if((kernelparams->setup_sects)>=(_32k/512) || // 0th sector not counted
162 kernelparams->boot_flag != 0xAA55)
163 die("Not a kernel");
164 heap_top = _rm_buf(m)+(_rm_size=0x200*(kernelparams->setup_sects+1)); // <*>
165 m->size -= _rm_size;
166 m->chunk_size -= _rm_size;
168 // Read remaining rm loader
169 if (readrm(m, _rm_size) == _rm_size) break;
170 }
171 die(kernel_file_error);
172 } while (0);
174 // Tell rm loader some info
176 if((int)vid_mode) kernelparams->vid_mode = vid_mode;
177 if((int)root_dev) kernelparams->root_dev = root_dev;
178 version_string = "";
180 #if 1
181 if(kernelparams->header == HdrS) { // starting linux 1.3.73
182 if(kernelparams->loadflags & 1) {
183 #else
184 if((kernelparams->header != HdrS) || (kernelparams->loadflags & 1) == 0)
185 die("I can't load bzImage low");
186 {
187 {
188 #endif
189 pm_high++;
191 // Hook on int15 to work around fn 88 DOS breakage
192 hook_int15_88();
193 }
194 if(kernelparams->kernel_version)
195 version_string = (char *) first1k+kernelparams->kernel_version+0x200;
196 kernelparams->type_of_loader = 0xff; // kernel do not know us (yet :-)
197 if(kernelparams->version >= 0x201) {
198 // * offset limit of the setup heap
199 // heap_end_ptr appears to be relative to the start of setup (ofs 0x0200)
200 kernelparams->heap_end_ptr = _32k-0x0200;
201 kernelparams->loadflags |= 0x80; // says to rm loader it's ok to use heap
202 }
203 // * if we will ever stop moving ourself to 0x90000
204 // we must say setup.S how much to move
205 //kernelparams->setup_move_size = _32k;
206 if(kernelparams->version >= 0x202) { // starting linux 2.4.0-test3-pre3
207 kernelparams->cmd_line_ptr = (((u32)(topseg()+0x0800))<<4);
208 goto cmd_line_ok;
209 }
210 }
211 first1k->cl_magic_ofs = 0x8000A33F;
212 }
214 cmd_line_ok:
215 // Check and enable A20 if needed
216 enable_a20_or_die();
218 // Read remaining kernel (pm part)
219 // Try to load kernel high, maybe even blindly storing it
220 // in unallocated memory as a last resort
222 {
223 struct image_himem *m = &pm;
224 if((u16)(((m->fallback=(u32((u16(_CS)+0x1FFF)&0xF000)<<4))+m->size)>>4) >
225 topseg() || _pm_high) {
226 m->fallback = base_himem;
227 }
229 load_image(m);
230 }
231 return version_string;
232 }
234 struct initrdparams_t {
235 u32 header; // 0202 Magic signature "HdrS"
236 u16 version; // 0206 Boot protocol version supported
237 u16 realmode_switch_ofs; // 0208 Hook called just before rm->pm
238 u16 realmode_switch_seg;
239 u16 start_sys_seg; // 020E
240 u16 kernel_version; // 020C Points to kernel version string
241 u8 type_of_loader; // 0210 Boot loader identifier
242 u8 loadflags; // 0211 Boot protocol option flags
243 u16 setup_move_size;// 0212 Move to high memory size (used with hooks)
244 u32 code32_start; // 0214 Boot loader hook (see below)
245 u32 initrd_buf; // 0218 initrd load address (set by boot loader)
246 u32 initrd_size; // 021C initrd size (set by boot loader)
247 };
249 // Read initrd if needed
250 void load_initrd() {
251 struct image_himem *m = &initrd;
252 if (!initrd_name && !m->fd) return;
253 #if defined(__BORLANDC__) && defined(NO386)
254 #pragma option -3
255 asm{
256 .386p
257 }
258 #endif
259 m->errmsg = "Can't use initrd file";
261 open_image(initrd_name, m);
263 if ((m->fallback=(memtop()-m->size)&(~PAGE_MASK))-m[-1].fallback < m[-1].size) {
264 close(m->fd);
265 puts(m->errmsg);
266 return;
267 }
269 load_image(m);
271 struct initrdparams_t *initrdparams = ((struct initrdparams_t *)&(((first1k_t*) _rm_buf(m-1))->params.header));
272 if(initrdparams->header == HdrS) {
273 initrdparams->initrd_buf = m->fallback;
274 initrdparams->initrd_size = m->size;
275 }
276 }