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

linld: large image support with VCPI
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Dec 22 21:06:17 2016 +0100 (2016-12-22)
parents 7f92b23984dc
children 23fc786c04e8
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 u8 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 u16 cl_magic; // 0020 commandline magic number (=0xA33F)
95 u16 cl_ofs; // 0022 commandline offset
96 u8 pad10[0x80-0x24];
97 // these two set by rm setup:
98 u8 hd0_disk_par[16]; // 0080 hd0-disk-parameter from intvector 0x41
99 u8 hd1_disk_par[16]; // 0090 hd1-disk-parameter from intvector 0x46
100 u8 pad20[0x01e0-0xa0];
101 // this is set by rm setup:
102 u32 alt_mem_size; // 01E0 extended memory size in Kb (from int 0x15 fn 0xe801)
103 u8 pad28[0x01f1-0x1e4];
104 struct kernelparams_t params;
105 }; //__attribute((packed));
107 #if sizeof(first1k_t)!=0x400
108 #error BUG: Bad first1k
109 #endif
111 const u32 HdrS = 'H' + ('d'<<8) + (u32('r')<<16) + (u32('S')<<24);
113 u8* rm_buf;
114 u16 rm_size;
115 u8 pm_high;
116 extern struct image_himem pm, initrd;
118 // register value to launch the kernel real mode code
119 #ifdef NO386
120 static u32 csip;
121 extern "C" u16 topseg();
122 #else
123 static u32 csip=0x90200000;
124 #define topseg() 0x9000
125 #endif
127 static const char kernel_file_error[] = "Can't use kernel file";
128 char* load_kernel() {
130 #ifdef NO386
131 csip=((u32)(topseg()+0x20)<<16);
132 #endif
133 // Open kernel, read first kb, check it
134 pm.errmsg = kernel_file_error;
135 open_image(kernel_name, &pm);
137 char *version_string;
138 {
139 struct first1k_t *first1k;
140 struct kernelparams_t *kernelparams;
141 first1k = (first1k_t*) (rm_buf = malloc_or_die(_32k));
142 kernelparams = &first1k->params;
143 {
144 u16 rm_seek;
146 // Do not use malloc below until heap_top adjustment (see <*>)
147 if (read(pm.fd, rm_buf, rm_seek=0x400) != 0x400) {
148 readfail:
149 die(kernel_file_error);
150 }
152 if(kernelparams->setup_sects == 0) {
153 #if 1
154 if(* (int *) &first1k->pad10[0x3F-0x24] == 0x3AE8) {
155 lseekset(pm.fd,rm_seek=0x200);
156 csip=((u32)topseg()<<16)+0x0042;
157 }
158 else
159 #endif
160 kernelparams->setup_sects=4;
161 }
162 rm_size = 0x200*(kernelparams->setup_sects+1); // 0th sector is not counted there
163 if(rm_size>_32k || kernelparams->boot_flag != 0xAA55)
164 die("It's not a kernel");
165 heap_top = rm_buf+rm_size; // <*>
167 // Read remaining rm loader
169 {
170 u16 cnt = rm_size-rm_seek;
171 if (read(pm.fd, rm_buf+rm_seek, cnt) != cnt) goto readfail;
172 }
173 }
175 // Tell rm loader some info
177 if(vid_mode) kernelparams->vid_mode = vid_mode;
178 if(root_dev) kernelparams->root_dev = root_dev;
179 version_string = 0;
181 #if 1
182 if(kernelparams->header == HdrS) { // starting linux 1.3.73
183 if(kernelparams->loadflags & 1) {
184 #else
185 if((kernelparams->header != HdrS) || (kernelparams->loadflags & 1) == 0)
186 die("I can't load bzImage low");
187 {
188 {
189 #endif
190 pm_high++;
192 // Hook on int15 to work around fn 88 DOS breakage
193 hook_int15_88();
195 // * will be called just before rm -> pm
196 extern void far last_ditch();
197 kernelparams->realmode_switch_ofs = ofs((void *)last_ditch);
198 kernelparams->realmode_switch_seg = seg(last_ditch);
199 }
200 if(kernelparams->kernel_version)
201 version_string = (char *) first1k+kernelparams->kernel_version+0x200;
202 kernelparams->type_of_loader = 0xff; // kernel do not know us (yet :-)
203 if(kernelparams->version >= 0x201) {
204 // * offset limit of the setup heap
205 // heap_end_ptr appears to be relative to the start of setup (ofs 0x0200)
206 kernelparams->heap_end_ptr = _32k-0x0200;
207 kernelparams->loadflags |= 0x80; // says to rm loader it's ok to use heap
208 }
209 // * if we will ever stop moving ourself to 0x90000
210 // we must say setup.S how much to move
211 //kernelparams->setup_move_size = _32k;
212 if(kernelparams->version >= 0x202) { // starting linux 2.4.0-test3-pre3
213 kernelparams->cmd_line_ptr = (((u32)(topseg()+0x0800))<<4);
214 goto cmd_line_ok;
215 }
216 }
217 first1k->cl_magic = 0xA33F;
218 first1k->cl_ofs = 0x8000;
219 }
221 cmd_line_ok:
222 // Check and enable A20 if needed
223 enable_a20_or_die();
225 // Read remaining kernel (pm part)
226 // Try to load kernel high, maybe even blindly storing it
227 // in unallocated memory as a last resort
229 {
230 struct image_himem *m = &pm;
231 m->fallback = (u32((u16(_CS)+0x1FFF)&0xF000)<<4);
232 m->size -= rm_size;
233 m->chunk_size -= rm_size;
234 if(m->fallback+m->size > (((u32)topseg())<<4) || pm_high) {
235 m->fallback = _1m+_64k;
236 }
238 load_image(m);
239 }
240 return version_string;
241 }
243 // Read initrd if needed
245 void load_initrd() {
246 struct image_himem *m = &initrd;
247 if (!initrd_name && !m->fd) return;
248 m->errmsg = "Can't use initrd file";
249 if (!pm.errmsg) {
250 noinitrd:
251 puts(m->errmsg);
252 return;
253 }
254 open_image(initrd_name, m);
256 m->fallback = (memtop()-m->size) & (~PAGE_MASK);
257 if (m->fallback < pm.fallback + pm.size) {
258 close(m->fd);
259 goto noinitrd;
260 }
262 load_image(m);
263 struct kernelparams_t *kernelparams = &(((first1k_t*) rm_buf)->params);
264 if(kernelparams->header == HdrS) {
265 kernelparams->initrd_buf = m->fallback;
266 kernelparams->initrd_size = m->size;
267 }
268 }
270 void boot_kernel() {
272 // Shrink stack: we won't need much of it now and have no malloc() plans
273 {
274 u16 new_SP=u16(heap_top)+0x100;
275 if(_SP>new_SP) _SP=new_SP;
276 }
277 if( (u16(_SP)>>4)+u16(_CS) >= topseg() ) {
278 // Oops! We can stomp on our toes... better stop now
279 die("Loaded too close to 9000:0");
280 }
282 cli(); // we start doing destructive things to DOS
284 // Move rm loader & commandline to 0x90000, Jump to kernel rm code
285 set_sregs_jump_seg_ofs(csip);
286 }