Add routine to initialize and test for the FPU.
[syslinux.git] / dos / syslinux.c
blobf3b08367a2cd350269489dd5c39f3b46cbd3264e
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * syslinux.c - Linux installer program for SYSLINUX
16 * Hacked up for DOS.
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "mystuff.h"
25 #include "syslinux.h"
26 #include "libfat.h"
28 const char *program = "syslinux"; /* Name of program */
29 uint16_t dos_version;
31 #ifdef DEBUG
32 # define dprintf printf
33 #else
34 # define dprintf(...) ((void)0)
35 #endif
37 void __attribute__((noreturn)) usage(void)
39 puts("Usage: syslinux [-sfma] <drive>: [bootsecfile]\n");
40 exit(1);
43 void unlock_device(int);
45 void __attribute__((noreturn)) die(const char *msg)
47 unlock_device(0);
48 puts("syslinux: ");
49 puts(msg);
50 putchar('\n');
51 exit(1);
55 * read/write wrapper functions
57 int creat(const char *filename, int mode)
59 uint16_t rv;
60 uint8_t err;
62 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
64 rv = 0x3C00;
65 asm volatile("int $0x21 ; setc %0"
66 : "=abcdm" (err), "+a" (rv)
67 : "c" (mode), "d" (filename));
68 if ( err ) {
69 dprintf("rv = %d\n", rv);
70 die("cannot open ldlinux.sys");
73 return rv;
76 void close(int fd)
78 uint16_t rv = 0x3E00;
80 dprintf("close(%d)\n", fd);
82 asm volatile("int $0x21"
83 : "+a" (rv)
84 : "b" (fd));
86 /* The only error MS-DOS returns for close is EBADF,
87 and we really don't care... */
90 ssize_t write_file(int fd, const void *buf, size_t count)
92 uint16_t rv;
93 ssize_t done = 0;
94 uint8_t err;
96 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
98 while ( count ) {
99 rv = 0x4000;
100 asm volatile("int $0x21 ; setc %0"
101 : "=abcdm" (err), "+a" (rv)
102 : "b" (fd), "c" (count), "d" (buf));
103 if ( err || rv == 0 )
104 die("file write error");
106 done += rv;
107 count -= rv;
110 return done;
113 static inline __attribute__((const)) uint16_t data_segment(void)
115 uint16_t ds;
117 asm("movw %%ds,%0" : "=rm" (ds));
118 return ds;
121 struct diskio {
122 uint32_t startsector;
123 uint16_t sectors;
124 uint16_t bufoffs, bufseg;
125 } __attribute__((packed));
127 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
129 uint8_t err;
130 struct diskio dio;
132 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
134 dio.startsector = sector;
135 dio.sectors = nsecs;
136 dio.bufoffs = (uintptr_t)buf;
137 dio.bufseg = data_segment();
139 asm volatile("int $0x26 ; setc %0 ; popfw"
140 : "=abcdm" (err)
141 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
143 if ( err )
144 die("sector write error");
147 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
149 uint8_t err;
150 struct diskio dio;
152 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
154 dio.startsector = sector;
155 dio.sectors = nsecs;
156 dio.bufoffs = (uintptr_t)buf;
157 dio.bufseg = data_segment();
159 asm volatile("int $0x25 ; setc %0 ; popfw"
160 : "=abcdm" (err)
161 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
163 if ( err )
164 die("sector read error");
167 /* Both traditional DOS and FAT32 DOS return this structure, but
168 FAT32 return a lot more data, so make sure we have plenty of space */
169 struct deviceparams {
170 uint8_t specfunc;
171 uint8_t devtype;
172 uint16_t devattr;
173 uint16_t cylinders;
174 uint8_t mediatype;
175 uint16_t bytespersec;
176 uint8_t secperclust;
177 uint16_t ressectors;
178 uint8_t fats;
179 uint16_t rootdirents;
180 uint16_t sectors;
181 uint8_t media;
182 uint16_t fatsecs;
183 uint16_t secpertrack;
184 uint16_t heads;
185 uint32_t hiddensecs;
186 uint32_t hugesectors;
187 uint8_t lotsofpadding[224];
188 } __attribute__((packed));
190 uint32_t get_partition_offset(int drive)
192 uint8_t err;
193 uint16_t rv;
194 struct deviceparams dp;
196 dp.specfunc = 1; /* Get current information */
198 rv = 0x440d;
199 asm volatile("int $0x21 ; setc %0"
200 : "=abcdm" (err), "+a" (rv), "=m" (dp)
201 : "b" (drive), "c" (0x0860), "d" (&dp));
203 if ( !err )
204 return dp.hiddensecs;
206 rv = 0x440d;
207 asm volatile("int $0x21 ; setc %0"
208 : "=abcdm" (err), "+a" (rv), "=m" (dp)
209 : "b" (drive), "c" (0x4860), "d" (&dp));
211 if ( !err )
212 return dp.hiddensecs;
214 die("could not find partition start offset");
217 struct rwblock {
218 uint8_t special;
219 uint16_t head;
220 uint16_t cylinder;
221 uint16_t firstsector;
222 uint16_t sectors;
223 uint16_t bufferoffset;
224 uint16_t bufferseg;
225 } __attribute__((packed));
227 static struct rwblock mbr = {
228 .special = 0,
229 .head = 0,
230 .cylinder = 0,
231 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
232 .sectors = 1,
233 .bufferoffset = 0,
234 .bufferseg = 0
237 void write_mbr(int drive, const void *buf)
239 uint16_t rv;
240 uint8_t err;
242 dprintf("write_mbr(%d,%p)\n", drive, buf);
244 mbr.bufferoffset = (uintptr_t)buf;
245 mbr.bufferseg = data_segment();
247 rv = 0x440d;
248 asm volatile("int $0x21 ; setc %0"
249 : "=abcdm" (err), "+a" (rv)
250 : "c" (0x0841), "d" (&mbr), "b" (drive), "m" (mbr));
252 if ( !err )
253 return;
255 rv = 0x440d;
256 asm volatile("int $0x21 ; setc %0"
257 : "=abcdm" (err), "+a" (rv)
258 : "c" (0x4841), "d" (&mbr), "b" (drive), "m" (mbr));
260 if ( err )
261 die("mbr write error");
264 void read_mbr(int drive, const void *buf)
266 uint16_t rv;
267 uint8_t err;
269 dprintf("read_mbr(%d,%p)\n", drive, buf);
271 mbr.bufferoffset = (uintptr_t)buf;
272 mbr.bufferseg = data_segment();
274 rv = 0x440d;
275 asm volatile("int $0x21 ; setc %0"
276 : "=abcdm" (err), "+a" (rv)
277 : "c" (0x0861), "d" (&mbr), "b" (drive), "m" (mbr));
279 if ( !err )
280 return;
282 rv = 0x440d;
283 asm volatile("int $0x21 ; setc %0"
284 : "=abcdm" (err), "+a" (rv)
285 : "c" (0x4861), "d" (&mbr), "b" (drive), "m" (mbr));
287 if ( err )
288 die("mbr read error");
291 /* This call can legitimately fail, and we don't care, so ignore error return */
292 void set_attributes(const char *file, int attributes)
294 uint16_t rv = 0x4301;
296 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
298 asm volatile("int $0x21"
299 : "+a" (rv)
300 : "c" (attributes), "d" (file));
304 * Version of the read_device function suitable for libfat
306 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
308 read_device(pp, buf, 1, sector);
309 return secsize;
312 static inline void get_dos_version(void)
314 uint16_t ver = 0x3001;
315 asm("int $0x21 ; xchgb %%ah,%%al" : "+a" (ver) : : "ebx", "ecx");
316 dos_version = ver;
317 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
320 /* The locking interface relies on static variables. A massive hack :( */
321 static uint16_t lock_level;
323 static inline void set_lock_device(uint8_t device)
325 lock_level = device;
328 void lock_device(int level)
330 uint16_t rv;
331 uint8_t err;
332 uint16_t lock_call;
334 if ( dos_version < 0x0700 )
335 return; /* Win9x/NT only */
337 #if 0
338 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
339 lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
340 #else
341 lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
342 #endif
344 while ( (lock_level >> 8) < level ) {
345 uint16_t new_level = lock_level + 0x0100;
346 dprintf("Trying lock %04x...\n", new_level);
347 rv = 0x444d;
348 asm volatile("int $0x21 ; setc %0"
349 : "=abcdm" (err), "+a" (rv)
350 : "b" (new_level), "c" (lock_call), "d"(0x0001));
351 if ( err ) {
352 /* rv == 0x0001 means this call is not supported, if so we
353 assume locking isn't needed (e.g. Win9x in DOS-only mode) */
354 if ( rv == 0x0001 )
355 return;
356 else
357 die("could not lock device");
360 lock_level = new_level;
362 return;
365 void unlock_device(int level)
367 uint16_t rv;
368 uint8_t err;
369 uint16_t unlock_call;
371 if ( dos_version < 0x0700 )
372 return; /* Win9x/NT only */
374 #if 0
375 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
376 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
377 #else
378 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
379 #endif
381 while ( (lock_level >> 8) > level ) {
382 uint16_t new_level = lock_level - 0x0100;
383 rv = 0x440d;
384 asm volatile("int $0x21 ; setc %0"
385 : "=abcdm" (err), "+a" (rv)
386 : "b" (new_level), "c" (unlock_call));
387 lock_level = new_level;
393 * This function does any desired MBR manipulation; called with the device lock held.
395 struct mbr_entry {
396 uint8_t active; /* Active flag */
397 uint8_t bhead; /* Begin head */
398 uint8_t bsector; /* Begin sector */
399 uint8_t bcylinder; /* Begin cylinder */
400 uint8_t filesystem; /* Filesystem value */
401 uint8_t ehead; /* End head */
402 uint8_t esector; /* End sector */
403 uint8_t ecylinder; /* End cylinder */
404 uint32_t startlba; /* Start sector LBA */
405 uint32_t sectors; /* Length in sectors */
406 } __attribute__((packed));
408 static void adjust_mbr(int device, int writembr, int set_active)
410 static unsigned char sectbuf[512];
411 int i;
413 if ( !writembr && !set_active )
414 return; /* Nothing to do */
416 read_mbr(device, sectbuf);
418 if ( writembr ) {
419 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
420 *(uint16_t *)(sectbuf+510) = 0xaa55;
423 if ( set_active ) {
424 uint32_t offset = get_partition_offset(device);
425 struct mbr_entry *me = (struct mbr_entry *)(sectbuf+446);
426 int found = 0;
428 for ( i = 0 ; i < 4 ; i++ ) {
429 if ( me->startlba == offset ) {
430 me->active = 0x80;
431 found++;
432 } else {
433 me->active = 0;
435 me++;
438 if ( found < 1 ) {
439 die("partition not found");
440 } else if ( found > 1 ) {
441 die("multiple aliased partitions found");
445 write_mbr(device, sectbuf);
448 int main(int argc, char *argv[])
450 static unsigned char sectbuf[512];
451 int dev_fd, fd;
452 static char ldlinux_name[] = "@:\\LDLINUX.SYS";
453 char **argp, *opt;
454 int force = 0; /* -f (force) option */
455 struct libfat_filesystem *fs;
456 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
457 int32_t ldlinux_cluster;
458 int nsectors;
459 const char *device = NULL, *bootsecfile = NULL;
460 const char *errmsg;
461 int i;
462 int writembr = 0; /* -m (write MBR) option */
463 int set_active = 0; /* -a (set partition active) option */
465 dprintf("argv = %p\n", argv);
466 for ( i = 0 ; i <= argc ; i++ )
467 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
469 (void)argc; /* Unused */
471 get_dos_version();
473 for ( argp = argv+1 ; *argp ; argp++ ) {
474 if ( **argp == '-' ) {
475 opt = *argp + 1;
476 if ( !*opt )
477 usage();
479 while ( *opt ) {
480 switch ( *opt ) {
481 case 's': /* Use "safe, slow and stupid" code */
482 syslinux_make_stupid();
483 break;
484 case 'f': /* Force install */
485 force = 1;
486 break;
487 case 'm': /* Write MBR */
488 writembr = 1;
489 break;
490 case 'a': /* Set partition active */
491 set_active = 1;
492 break;
493 default:
494 usage();
496 opt++;
498 } else {
499 if ( bootsecfile )
500 usage();
501 else if ( device )
502 bootsecfile = *argp;
503 else
504 device = *argp;
508 if ( !device )
509 usage();
512 * Figure out which drive we're talking to
514 dev_fd = (device[0] & ~0x20) - 0x40;
515 if ( dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2] )
516 usage();
518 set_lock_device(dev_fd);
520 lock_device(2); /* Make sure we can lock the device */
521 read_device(dev_fd, sectbuf, 1, 0);
522 unlock_device(1);
525 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
527 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
528 unlock_device(0);
529 puts(errmsg);
530 putchar('\n');
531 exit(1);
534 ldlinux_name[0] = dev_fd | 0x40;
536 set_attributes(ldlinux_name, 0);
537 fd = creat(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
538 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
539 close(fd);
542 * Now, use libfat to create a block map. This probably
543 * should be changed to use ioctl(...,FIBMAP,...) since
544 * this is supposed to be a simple, privileged version
545 * of the installer.
547 lock_device(2);
548 fs = libfat_open(libfat_xpread, dev_fd);
549 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
550 secp = sectors;
551 nsectors = 0;
552 s = libfat_clustertosector(fs, ldlinux_cluster);
553 while ( s && nsectors < 65 ) {
554 *secp++ = s;
555 nsectors++;
556 s = libfat_nextsector(fs, s);
558 libfat_close(fs);
561 * Patch ldlinux.sys and the boot sector
563 syslinux_patch(sectors, nsectors);
566 * Write the now-patched first sector of ldlinux.sys
568 lock_device(3);
569 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
572 * Muck with the MBR, if desired, while we hold the lock
574 adjust_mbr(dev_fd, writembr, set_active);
577 * To finish up, write the boot sector
580 /* Read the superblock again since it might have changed while mounted */
581 read_device(dev_fd, sectbuf, 1, 0);
583 /* Copy the syslinux code into the boot sector */
584 syslinux_make_bootsect(sectbuf);
586 /* Write new boot sector */
587 if ( bootsecfile ) {
588 unlock_device(0);
589 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
590 write_file(fd, sectbuf, 512);
591 close(fd);
592 } else {
593 write_device(dev_fd, sectbuf, 1, 0);
594 unlock_device(0);
597 /* Done! */
599 return 0;