wok annotate linld/stuff/src/LOAD.CPP @ rev 19538

linld: add 'linld <kernel> <cmdline>' syntax
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Dec 02 12:37:59 2016 +0100 (2016-12-02)
parents bb42796dcd3b
children e428345df29a
rev   line source
pascal@19515 1 // This file is distributed under GPL
pascal@19515 2
pascal@19515 3 #include "crtl.h"
pascal@19515 4 #include "common.h"
pascal@19515 5
pascal@19515 6 /***************
pascal@19515 7 Memory layout assumed by kernel boot process
pascal@19515 8 --------------------------------------------
pascal@19515 9 Note: claims that kernel setup is relocatable are
pascal@19515 10 still not 100% valid:
pascal@19515 11 bzImage decompressing trashes 10000-8ffff range,
pascal@19515 12 so rm loader -> pm kernel info is lost if it was here...
pascal@19515 13 So I had to stick to 90000.
pascal@19515 14
pascal@19515 15 10000000+------------------------+ <- 256m
pascal@19515 16 | initrd | initrd is at top of mem, but
pascal@19515 17 | | not higher than 256m
pascal@19515 18 +------------------------+
pascal@19515 19 +------------------------+
pascal@19515 20 | bzImage | bzImage is at 1m
pascal@19515 21 | | VCPI/XMS/64k offset tricks used...
pascal@19515 22 00100000+------------------------+ <- 1m
pascal@19515 23 | video, BIOS etc | Do not use.
pascal@19515 24 000A0000+------------------------+
pascal@19515 25 | Reserved for BIOS | Do not use. Reserved for BIOS EBDA.
pascal@19515 26 0009A000+------------------------+ <- stack top for kernel rm code
pascal@19515 27 | Cmdline |
pascal@19515 28 00098000+------------------------+ <- heap top for kernel rm code
pascal@19515 29 | Kernel setup | The kernel real-mode code.
pascal@19515 30 00090200+------------------------+
pascal@19515 31 | Kernel boot sector | The kernel legacy boot sector.
pascal@19515 32 00090000+------------------------+
pascal@19515 33 | Zapped by ungzip | Historically zImages were loaded here
pascal@19515 34 | (zImage once was here) | bzImages use this space for ungzipping
pascal@19515 35 00010000+------------------------+
pascal@19515 36 | Boot loader | <- Boot sector entry point 0000:7C00
pascal@19515 37 00001000+------------------------+
pascal@19515 38 | Reserved for MBR/BIOS |
pascal@19515 39 00000800+------------------------+
pascal@19515 40 | Typically used by MBR |
pascal@19515 41 00000600+------------------------+
pascal@19515 42 | BIOS use only |
pascal@19515 43 00000000+------------------------+
pascal@19515 44 */
pascal@19515 45
pascal@19538 46 struct kernelparams_t {
pascal@19538 47 u8 pad0;
pascal@19515 48 u8 setup_sects; // 01F1 The size of the setup in sectors
pascal@19515 49 // boot sector is NOT included here
pascal@19515 50 u16 ro_flag; // 01F2 If set, the root is mounted readonly
pascal@19515 51 u16 syssize; // 01F4 DO NOT USE - for bootsect.S use only:
pascal@19515 52 // size of pm part of kernel
pascal@19515 53 // (in 16 byte units, rounded up)
pascal@19515 54 u16 swap_dev; // 01F6 DO NOT USE - obsolete
pascal@19515 55 u16 ram_size; // 01F8 DO NOT USE - for bootsect.S use only:
pascal@19515 56 // if nonzero then kernel
pascal@19515 57 // (driver/block/ramdisk.c: rd_load())
pascal@19515 58 // will try to load the contents for the ramdisk
pascal@19515 59 // from the "root_dev" which MUST then have the
pascal@19515 60 // floppyMAJOR
pascal@19515 61 // The file-system on that floppy must be MINIX
pascal@19515 62 // If rd_load() succeeds it sets the root_dev
pascal@19515 63 // to the ramdisk for mounting it
pascal@19515 64 u16 vid_mode; // 01FA Video mode control
pascal@19515 65 u16 root_dev; // 01FC Default root device number
pascal@19515 66 u16 boot_flag; // 01FE 0xAA55 magic number
pascal@19515 67 u16 jump; // 0200 Jump instruction
pascal@19515 68 u32 header; // 0202 Magic signature "HdrS"
pascal@19515 69 u16 version; // 0206 Boot protocol version supported
pascal@19515 70 u16 realmode_switch_ofs; // 0208 Hook called just before rm->pm
pascal@19515 71 u16 realmode_switch_seg;
pascal@19515 72 u16 start_sys_seg; // 020E
pascal@19515 73 u16 kernel_version; // 020C Points to kernel version string
pascal@19515 74 u8 type_of_loader; // 0210 Boot loader identifier
pascal@19515 75 u8 loadflags; // 0211 Boot protocol option flags
pascal@19515 76 u16 setup_move_size;// 0212 Move to high memory size (used with hooks)
pascal@19515 77 u32 code32_start; // 0214 Boot loader hook (see below)
pascal@19515 78 u32 initrd_buf; // 0218 initrd load address (set by boot loader)
pascal@19515 79 u32 initrd_size; // 021C initrd size (set by boot loader)
pascal@19515 80 u32 bootsect_kludge;// 0220 DO NOT USE - for bootsect.S use only
pascal@19515 81 u16 heap_end_ptr; // 0224 Free memory after setup end
pascal@19515 82 u16 pad1; // 0226 Unused
pascal@19515 83 u32 cmd_line_ptr; // 0228 32-bit pointer to the kernel command line
pascal@19515 84 u8 pad30[0x400-0x22c]; // 022C
pascal@19515 85 // 02D0 up to 32 20-byte mem info structs from
pascal@19515 86 // int 0x15 fn 0xe820
pascal@19515 87 }; //__attribute((packed));
pascal@19515 88
pascal@19538 89 struct first1k_t {
pascal@19538 90 // these two set by rm setup:
pascal@19538 91 u16 curr_curs; // 0000 saved cursor position
pascal@19538 92 u16 ext_mem_size; // 0002 extended memory size in Kb (from int 0x15 fn 0x88)
pascal@19538 93 u8 pad00[0x20-4];
pascal@19538 94 // old-style cmdline (not used in LINLD (yet?))
pascal@19538 95 u16 cl_magic; // 0020 commandline magic number (=0xA33F)
pascal@19538 96 u16 cl_ofs; // 0022 commandline offset
pascal@19538 97 u8 pad10[0x80-0x24];
pascal@19538 98 // these two set by rm setup:
pascal@19538 99 u8 hd0_disk_par[16]; // 0080 hd0-disk-parameter from intvector 0x41
pascal@19538 100 u8 hd1_disk_par[16]; // 0090 hd1-disk-parameter from intvector 0x46
pascal@19538 101 u8 pad20[0x01e0-0xa0];
pascal@19538 102 // this is set by rm setup:
pascal@19538 103 u32 alt_mem_size; // 01E0 extended memory size in Kb (from int 0x15 fn 0xe801)
pascal@19538 104 u8 pad28[0x01f0-0x1e4];
pascal@19538 105 struct kernelparams_t params;
pascal@19538 106 }; //__attribute((packed));
pascal@19538 107
pascal@19515 108 #if sizeof(first1k_t)!=0x400
pascal@19515 109 #error BUG: Bad first1k
pascal@19515 110 #endif
pascal@19515 111
pascal@19515 112 const u32 HdrS = 'H' + ('d'<<8) + (u32('r')<<16) + (u32('S')<<24);
pascal@19515 113
pascal@19515 114 u8* rm_buf;
pascal@19515 115 static u16 rm_size;
pascal@19515 116 u8 pm_high;
pascal@19515 117 struct image_himem pm;
pascal@19515 118 struct image_himem initrd;
pascal@19515 119
pascal@19515 120 static void memcpy_image(struct image_himem *m) {
pascal@19515 121 if (m->fallback != m->buf)
pascal@19515 122 memcpy32(
pascal@19538 123 m->fallback, // dst seg,ofs
pascal@19515 124 0, m->buf, // src seg,ofs
pascal@19515 125 m->size // size
pascal@19515 126 );
pascal@19515 127 }
pascal@19515 128
pascal@19515 129 // Called from inside kernel just before rm->pm
pascal@19515 130 // _loadds _saveregs: done by hand
pascal@19515 131 void far last_ditch() {
pascal@19515 132 cli(); // we start doing *really* destructive things to DOS/BIOS
pascal@19515 133 // it means: do not even try to enable ints
pascal@19515 134 // or call BIOS services after this
pascal@19515 135 asm {
pascal@19515 136 push ds
pascal@19515 137 push cs
pascal@19515 138 pop ds
pascal@19515 139 #ifndef NO386
pascal@19515 140 pusha
pascal@19515 141 #else
pascal@19515 142 push ax
pascal@19515 143 push bx
pascal@19515 144 push cx
pascal@19515 145 push dx
pascal@19515 146 #endif
pascal@19515 147 }
pascal@19515 148 if(pm.fallback > _1m) pm.fallback = _1m;
pascal@19515 149 if(vcpi==0) {
pascal@19515 150 // Move kernel
pascal@19515 151 memcpy_image(&pm);
pascal@19515 152 // Move initrd
pascal@19515 153 memcpy_image(&initrd);
pascal@19515 154 } else { //vcpi
pascal@19515 155 vm2rm();
pascal@19515 156 // Move kernel
pascal@19515 157 // 'Gathering' copy in chunks of PAGE_SIZE
pascal@19515 158 // No risk of overlapping: kernel is copied from above to 1m mark
pascal@19538 159 pm.size = initrd.size = PAGE_SIZE;
pascal@19515 160 u32 *p = pm.bufv;
pascal@19515 161 if (p) while(*p) {
pascal@19515 162 pm.buf = *p;
pascal@19515 163 memcpy_image(&pm);
pascal@19515 164 p++; pm.fallback+=PAGE_SIZE;
pascal@19515 165 }
pascal@19515 166 // Move initrd
pascal@19515 167 if(initrd.fallback) {
pascal@19515 168 // This is tricky: copy initrd backwards to reduce
pascal@19515 169 // risk of overlapping: use the fact that initrd is copied
pascal@19515 170 // to the very top of ram
pascal@19515 171 // (overlapping still can happen with more than 256mb ram)
pascal@19515 172 // (generic solution for this overwrite problem, anyone?)
pascal@19515 173 p=initrd.bufv;
pascal@19515 174 do {
pascal@19515 175 p++; initrd.fallback+=PAGE_SIZE;
pascal@19515 176 } while(*p);
pascal@19515 177 do {
pascal@19515 178 p--; initrd.fallback-=PAGE_SIZE;
pascal@19515 179 initrd.buf = *p;
pascal@19515 180 memcpy_image(&initrd);
pascal@19515 181 } while(p != initrd.bufv);
pascal@19515 182 }
pascal@19515 183 }
pascal@19515 184 asm {
pascal@19515 185 #ifndef NO386
pascal@19515 186 popa
pascal@19515 187 #else
pascal@19515 188 pop dx
pascal@19515 189 pop cx
pascal@19515 190 pop bx
pascal@19515 191 pop ax
pascal@19515 192 #endif
pascal@19515 193 pop ds
pascal@19515 194 }
pascal@19515 195 }
pascal@19515 196
pascal@19515 197 // register value to launch the kernel real mode code
pascal@19515 198 #ifdef NO386
pascal@19515 199 static u32 sssp;
pascal@19515 200 static u32 csip;
pascal@19515 201 extern "C" u16 topseg();
pascal@19515 202 #else
pascal@19515 203 const u32 sssp=0x9000A000;
pascal@19515 204 static u32 csip=0x90200000;
pascal@19515 205 #define topseg() 0x9000
pascal@19515 206 #endif
pascal@19515 207
pascal@19515 208 static const char kernel_file_error[] = "Can't use kernel file";
pascal@19515 209 char* load_kernel() {
pascal@19515 210
pascal@19515 211 #ifdef NO386
pascal@19515 212 sssp=((u32)topseg()<<16)+0xA000;
pascal@19515 213 csip=((u32)(topseg()+0x20)<<16);
pascal@19515 214 #endif
pascal@19515 215 // Open kernel, read first kb, check it
pascal@19515 216 pm.errmsg = kernel_file_error;
pascal@19515 217 open_image(kernel_name, &pm);
pascal@19515 218
pascal@19515 219 char *version_string;
pascal@19515 220 {
pascal@19515 221 struct first1k_t *first1k;
pascal@19538 222 struct kernelparams_t *kernelparams;
pascal@19515 223 first1k = (first1k_t*) (rm_buf = malloc_or_die(_32k));
pascal@19538 224 kernelparams = &first1k->params;
pascal@19515 225 {
pascal@19515 226 u16 rm_seek;
pascal@19515 227
pascal@19515 228 // Do not use malloc below until heap_top adjustment (see <*>)
pascal@19515 229 if (read(pm.fd, rm_buf, rm_seek=0x400) != 0x400) {
pascal@19515 230 readfail:
pascal@19515 231 die(kernel_file_error);
pascal@19515 232 }
pascal@19515 233
pascal@19538 234 if(!kernelparams->setup_sects) {
pascal@19515 235 #if 1
pascal@19515 236 if(* (int *) &first1k->pad10[0x3F-0x24] == 0x3AE8) {
pascal@19515 237 lseek(pm.fd,rm_seek=0x200,SEEK_SET);
pascal@19515 238 csip=((u32)topseg()<<16)+0x0042;
pascal@19515 239 }
pascal@19515 240 else
pascal@19515 241 #endif
pascal@19538 242 kernelparams->setup_sects=4;
pascal@19515 243 }
pascal@19538 244 rm_size = 0x200*(kernelparams->setup_sects+1); // 0th sector is not counted there
pascal@19538 245 if(rm_size>_32k || kernelparams->boot_flag != 0xAA55)
pascal@19515 246 die("It's not a kernel");
pascal@19515 247 heap_top = rm_buf+rm_size; // <*>
pascal@19515 248
pascal@19515 249 // Read remaining rm loader
pascal@19515 250
pascal@19515 251 {
pascal@19515 252 u16 cnt = rm_size-rm_seek;
pascal@19515 253 if (read(pm.fd, rm_buf+rm_seek, cnt) != cnt) goto readfail;
pascal@19515 254 }
pascal@19515 255 }
pascal@19515 256
pascal@19515 257 // Tell rm loader some info
pascal@19515 258
pascal@19538 259 if(vid_mode) kernelparams->vid_mode = vid_mode;
pascal@19538 260 if(root_dev) kernelparams->root_dev = root_dev;
pascal@19515 261 version_string = 0;
pascal@19515 262
pascal@19515 263 #if 1
pascal@19538 264 if(kernelparams->header == HdrS) { // starting linux 1.3.73
pascal@19538 265 if(kernelparams->loadflags & 1) {
pascal@19515 266 #else
pascal@19538 267 if((kernelparams->header != HdrS) || (kernelparams->loadflags & 1) == 0)
pascal@19515 268 die("I can't load bzImage low");
pascal@19515 269 {
pascal@19515 270 {
pascal@19515 271 #endif
pascal@19515 272 pm_high++;
pascal@19515 273
pascal@19515 274 // Hook on int15 to work around fn 88 DOS breakage
pascal@19515 275 hook_int15_88();
pascal@19515 276
pascal@19515 277 // * will be called just before rm -> pm
pascal@19538 278 kernelparams->realmode_switch_ofs = ofs(last_ditch);
pascal@19538 279 kernelparams->realmode_switch_seg = seg(last_ditch);
pascal@19515 280 }
pascal@19538 281 if(kernelparams->kernel_version)
pascal@19538 282 version_string = (char *) first1k+kernelparams->kernel_version+0x200;
pascal@19538 283 kernelparams->type_of_loader = 0xff; // kernel do not know us (yet :-)
pascal@19538 284 if(kernelparams->version >= 0x201) {
pascal@19515 285 // * offset limit of the setup heap
pascal@19515 286 // heap_end_ptr appears to be relative to the start of setup (ofs 0x0200)
pascal@19538 287 kernelparams->heap_end_ptr = _32k-0x0200;
pascal@19538 288 kernelparams->loadflags |= 0x80; // says to rm loader it's ok to use heap
pascal@19515 289 }
pascal@19515 290 // * if we will ever stop moving ourself to 0x90000
pascal@19515 291 // we must say setup.S how much to move
pascal@19538 292 //kernelparams->setup_move_size = _32k;
pascal@19538 293 if(kernelparams->version >= 0x202) { // starting linux 2.4.0-test3-pre3
pascal@19538 294 kernelparams->cmd_line_ptr = (((u32)(topseg()+0x0800))<<4);
pascal@19515 295 goto cmd_line_ok;
pascal@19515 296 }
pascal@19515 297 }
pascal@19515 298 first1k->cl_magic = 0xA33F;
pascal@19515 299 first1k->cl_ofs = 0x8000;
pascal@19515 300 }
pascal@19515 301
pascal@19515 302 cmd_line_ok:
pascal@19515 303 // Check and enable A20 if needed
pascal@19515 304 enable_a20_or_die();
pascal@19515 305
pascal@19515 306 // Read remaining kernel (pm part)
pascal@19515 307 // Try to load kernel high, maybe even blindly storing it
pascal@19515 308 // in unallocated memory as a last resort
pascal@19515 309
pascal@19515 310 pm.fallback = (u32((u16(_CS)+0x1FFF)&0xF000)<<4);
pascal@19515 311 pm.size -= rm_size;
pascal@19538 312 pm.chunk_size -= rm_size;
pascal@19515 313 if(pm.fallback+pm.size > (((u32)topseg())<<4) || pm_high) {
pascal@19515 314 pm.fallback = _1m+_64k;
pascal@19515 315 }
pascal@19515 316
pascal@19515 317 load_image(&pm);
pascal@19515 318 return version_string;
pascal@19515 319 }
pascal@19515 320
pascal@19515 321 // Read initrd if needed
pascal@19515 322
pascal@19515 323 void load_initrd() {
pascal@19515 324 struct image_himem *m = &initrd;
pascal@19515 325 if (!initrd_name && !initrd.fd) return;
pascal@19538 326 m->errmsg = "Can't use initrd file";
pascal@19538 327 if (!pm.errmsg) {
pascal@19515 328 noinitrd:
pascal@19538 329 puts(m->errmsg);
pascal@19515 330 return;
pascal@19515 331 }
pascal@19515 332 open_image(initrd_name, m);
pascal@19515 333
pascal@19515 334 m->fallback = (memtop()-m->size) & (~PAGE_MASK);
pascal@19515 335 if (m->fallback < pm.fallback + pm.size) {
pascal@19515 336 close(m->fd);
pascal@19515 337 goto noinitrd;
pascal@19515 338 }
pascal@19515 339
pascal@19515 340 load_image(m);
pascal@19538 341 struct kernelparams_t *kernelparams = (kernelparams_t *)(rm_buf+0x1F0);
pascal@19538 342 if(kernelparams->header == HdrS) {
pascal@19538 343 kernelparams->initrd_buf = m->fallback;
pascal@19538 344 kernelparams->initrd_size = m->size;
pascal@19515 345 }
pascal@19515 346 }
pascal@19515 347
pascal@19515 348 void boot_kernel() {
pascal@19515 349
pascal@19515 350 // Shrink stack: we won't need much of it now and have no malloc() plans
pascal@19515 351 {
pascal@19515 352 u16 new_SP=u16(heap_top)+0x100;
pascal@19515 353 if(_SP>new_SP) _SP=new_SP;
pascal@19515 354 }
pascal@19515 355 if( u16(_CS)+(u16(_SP)>>4) >= topseg() ) {
pascal@19515 356 // Oops! We can stomp on our toes... better stop now
pascal@19515 357 die("Loaded too close to 9000:0");
pascal@19515 358 }
pascal@19515 359
pascal@19515 360 cli(); // we start doing destructive things to DOS
pascal@19515 361
pascal@19515 362 // Move rm loader & commandline to 0x90000
pascal@19538 363 // overkill: copy PAGE_SIZE bytes
pascal@19538 364 rmcpy(rm_buf, rm_size);
pascal@19515 365
pascal@19515 366 // Jump to kernel rm code
pascal@19515 367 set_sregs_jump_seg_ofs(csip, sssp);
pascal@19515 368 }