Adding upstream version 3.51.
[syslinux-debian/hramrach.git] / dos / syslinux.c
blob58cc97ee7da1aec36d1caf075d3b4e61573c7d54
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2007 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 [-sfmar][-d directory] <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);
54 void warning(const char *msg)
56 puts("syslinux: warning: ");
57 puts(msg);
58 putchar('\n');
62 * read/write wrapper functions
64 int creat(const char *filename, int mode)
66 uint16_t rv;
67 uint8_t err;
69 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
71 rv = 0x3C00;
72 asm volatile("int $0x21 ; setc %0"
73 : "=bcdm" (err), "+a" (rv)
74 : "c" (mode), "d" (filename));
75 if ( err ) {
76 dprintf("rv = %d\n", rv);
77 die("cannot open ldlinux.sys");
80 return rv;
83 void close(int fd)
85 uint16_t rv = 0x3E00;
87 dprintf("close(%d)\n", fd);
89 asm volatile("int $0x21"
90 : "+a" (rv)
91 : "b" (fd));
93 /* The only error MS-DOS returns for close is EBADF,
94 and we really don't care... */
97 int rename(const char *oldname, const char *newname)
99 uint16_t rv = 0x5600; /* Also support 43FFh? */
100 uint8_t err;
102 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
104 asm volatile("int $0x21 ; setc %0"
105 : "=bcdm" (err), "+a" (rv)
106 : "d" (oldname), "D" (newname));
108 if ( err ) {
109 dprintf("rv = %d\n", rv);
110 warning("cannot move ldlinux.sys");
111 return rv;
114 return 0;
117 ssize_t write_file(int fd, const void *buf, size_t count)
119 uint16_t rv;
120 ssize_t done = 0;
121 uint8_t err;
123 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
125 while ( count ) {
126 rv = 0x4000;
127 asm volatile("int $0x21 ; setc %0"
128 : "=abcdm" (err), "+a" (rv)
129 : "b" (fd), "c" (count), "d" (buf));
130 if ( err || rv == 0 )
131 die("file write error");
133 done += rv;
134 count -= rv;
137 return done;
140 static inline __attribute__((const)) uint16_t data_segment(void)
142 uint16_t ds;
144 asm("movw %%ds,%0" : "=rm" (ds));
145 return ds;
148 struct diskio {
149 uint32_t startsector;
150 uint16_t sectors;
151 uint16_t bufoffs, bufseg;
152 } __attribute__((packed));
154 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
156 uint8_t err;
157 struct diskio dio;
159 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
161 dio.startsector = sector;
162 dio.sectors = nsecs;
163 dio.bufoffs = (uintptr_t)buf;
164 dio.bufseg = data_segment();
166 asm volatile("int $0x26 ; setc %0 ; popfw"
167 : "=abcdm" (err)
168 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
170 if ( err )
171 die("sector write error");
174 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
176 uint8_t err;
177 struct diskio dio;
179 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
181 dio.startsector = sector;
182 dio.sectors = nsecs;
183 dio.bufoffs = (uintptr_t)buf;
184 dio.bufseg = data_segment();
186 asm volatile("int $0x25 ; setc %0 ; popfw"
187 : "=abcdm" (err)
188 : "a" (drive-1), "b" (&dio), "c" (-1), "d" (buf), "m" (dio));
190 if ( err )
191 die("sector read error");
194 /* Both traditional DOS and FAT32 DOS return this structure, but
195 FAT32 return a lot more data, so make sure we have plenty of space */
196 struct deviceparams {
197 uint8_t specfunc;
198 uint8_t devtype;
199 uint16_t devattr;
200 uint16_t cylinders;
201 uint8_t mediatype;
202 uint16_t bytespersec;
203 uint8_t secperclust;
204 uint16_t ressectors;
205 uint8_t fats;
206 uint16_t rootdirents;
207 uint16_t sectors;
208 uint8_t media;
209 uint16_t fatsecs;
210 uint16_t secpertrack;
211 uint16_t heads;
212 uint32_t hiddensecs;
213 uint32_t hugesectors;
214 uint8_t lotsofpadding[224];
215 } __attribute__((packed));
217 uint32_t get_partition_offset(int drive)
219 uint8_t err;
220 uint16_t rv;
221 struct deviceparams dp;
223 dp.specfunc = 1; /* Get current information */
225 rv = 0x440d;
226 asm volatile("int $0x21 ; setc %0"
227 : "=abcdm" (err), "+a" (rv), "=m" (dp)
228 : "b" (drive), "c" (0x0860), "d" (&dp));
230 if ( !err )
231 return dp.hiddensecs;
233 rv = 0x440d;
234 asm volatile("int $0x21 ; setc %0"
235 : "=abcdm" (err), "+a" (rv), "=m" (dp)
236 : "b" (drive), "c" (0x4860), "d" (&dp));
238 if ( !err )
239 return dp.hiddensecs;
241 die("could not find partition start offset");
244 struct rwblock {
245 uint8_t special;
246 uint16_t head;
247 uint16_t cylinder;
248 uint16_t firstsector;
249 uint16_t sectors;
250 uint16_t bufferoffset;
251 uint16_t bufferseg;
252 } __attribute__((packed));
254 static struct rwblock mbr = {
255 .special = 0,
256 .head = 0,
257 .cylinder = 0,
258 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
259 .sectors = 1,
260 .bufferoffset = 0,
261 .bufferseg = 0
264 void write_mbr(int drive, const void *buf)
266 uint16_t rv;
267 uint8_t err;
269 dprintf("write_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" (0x0841), "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" (0x4841), "d" (&mbr), "b" (drive), "m" (mbr));
287 if ( err )
288 die("mbr write error");
291 void read_mbr(int drive, const void *buf)
293 uint16_t rv;
294 uint8_t err;
296 dprintf("read_mbr(%d,%p)\n", drive, buf);
298 mbr.bufferoffset = (uintptr_t)buf;
299 mbr.bufferseg = data_segment();
301 rv = 0x440d;
302 asm volatile("int $0x21 ; setc %0"
303 : "=abcdm" (err), "+a" (rv)
304 : "c" (0x0861), "d" (&mbr), "b" (drive), "m" (mbr));
306 if ( !err )
307 return;
309 rv = 0x440d;
310 asm volatile("int $0x21 ; setc %0"
311 : "=abcdm" (err), "+a" (rv)
312 : "c" (0x4861), "d" (&mbr), "b" (drive), "m" (mbr));
314 if ( err )
315 die("mbr read error");
318 /* This call can legitimately fail, and we don't care, so ignore error return */
319 void set_attributes(const char *file, int attributes)
321 uint16_t rv = 0x4301;
323 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
325 asm volatile("int $0x21"
326 : "+a" (rv)
327 : "c" (attributes), "d" (file));
331 * Version of the read_device function suitable for libfat
333 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
335 read_device(pp, buf, 1, sector);
336 return secsize;
339 static inline void get_dos_version(void)
341 uint16_t ver = 0x3001;
342 asm("int $0x21 ; xchgb %%ah,%%al" : "+a" (ver) : : "ebx", "ecx");
343 dos_version = ver;
344 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
347 /* The locking interface relies on static variables. A massive hack :( */
348 static uint16_t lock_level;
350 static inline void set_lock_device(uint8_t device)
352 lock_level = device;
355 void lock_device(int level)
357 uint16_t rv;
358 uint8_t err;
359 uint16_t lock_call;
361 if ( dos_version < 0x0700 )
362 return; /* Win9x/NT only */
364 #if 0
365 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
366 lock_call = (dos_version >= 0x0710) ? 0x484A : 0x084A;
367 #else
368 lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
369 #endif
371 while ( (lock_level >> 8) < level ) {
372 uint16_t new_level = lock_level + 0x0100;
373 dprintf("Trying lock %04x...\n", new_level);
374 rv = 0x444d;
375 asm volatile("int $0x21 ; setc %0"
376 : "=abcdm" (err), "+a" (rv)
377 : "b" (new_level), "c" (lock_call), "d"(0x0001));
378 if ( err ) {
379 /* rv == 0x0001 means this call is not supported, if so we
380 assume locking isn't needed (e.g. Win9x in DOS-only mode) */
381 if ( rv == 0x0001 )
382 return;
383 else
384 die("could not lock device");
387 lock_level = new_level;
389 return;
392 void unlock_device(int level)
394 uint16_t rv;
395 uint8_t err;
396 uint16_t unlock_call;
398 if ( dos_version < 0x0700 )
399 return; /* Win9x/NT only */
401 #if 0
402 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
403 unlock_call = (dos_version >= 0x0710) ? 0x486A : 0x086A;
404 #else
405 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
406 #endif
408 while ( (lock_level >> 8) > level ) {
409 uint16_t new_level = lock_level - 0x0100;
410 rv = 0x440d;
411 asm volatile("int $0x21 ; setc %0"
412 : "=abcdm" (err), "+a" (rv)
413 : "b" (new_level), "c" (unlock_call));
414 lock_level = new_level;
420 * This function does any desired MBR manipulation; called with the device lock held.
422 struct mbr_entry {
423 uint8_t active; /* Active flag */
424 uint8_t bhead; /* Begin head */
425 uint8_t bsector; /* Begin sector */
426 uint8_t bcylinder; /* Begin cylinder */
427 uint8_t filesystem; /* Filesystem value */
428 uint8_t ehead; /* End head */
429 uint8_t esector; /* End sector */
430 uint8_t ecylinder; /* End cylinder */
431 uint32_t startlba; /* Start sector LBA */
432 uint32_t sectors; /* Length in sectors */
433 } __attribute__((packed));
435 static void adjust_mbr(int device, int writembr, int set_active)
437 static unsigned char sectbuf[512];
438 int i;
440 if ( !writembr && !set_active )
441 return; /* Nothing to do */
443 read_mbr(device, sectbuf);
445 if ( writembr ) {
446 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
447 *(uint16_t *)(sectbuf+510) = 0xaa55;
450 if ( set_active ) {
451 uint32_t offset = get_partition_offset(device);
452 struct mbr_entry *me = (struct mbr_entry *)(sectbuf+446);
453 int found = 0;
455 for ( i = 0 ; i < 4 ; i++ ) {
456 if ( me->startlba == offset ) {
457 me->active = 0x80;
458 found++;
459 } else {
460 me->active = 0;
462 me++;
465 if ( found < 1 ) {
466 die("partition not found");
467 } else if ( found > 1 ) {
468 die("multiple aliased partitions found");
472 write_mbr(device, sectbuf);
475 int main(int argc, char *argv[])
477 static unsigned char sectbuf[512];
478 int dev_fd, fd;
479 static char ldlinux_name[] = "@:\\ldlinux.sys";
480 char **argp, *opt;
481 int force = 0; /* -f (force) option */
482 struct libfat_filesystem *fs;
483 libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
484 int32_t ldlinux_cluster;
485 int nsectors;
486 const char *device = NULL, *bootsecfile = NULL;
487 const char *errmsg;
488 int i;
489 int writembr = 0; /* -m (write MBR) option */
490 int set_active = 0; /* -a (set partition active) option */
491 const char *subdir = NULL;
492 int stupid = 0;
493 int raid_mode = 0;
495 dprintf("argv = %p\n", argv);
496 for ( i = 0 ; i <= argc ; i++ )
497 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
499 (void)argc; /* Unused */
501 get_dos_version();
503 for ( argp = argv+1 ; *argp ; argp++ ) {
504 if ( **argp == '-' ) {
505 opt = *argp + 1;
506 if ( !*opt )
507 usage();
509 while ( *opt ) {
510 switch ( *opt ) {
511 case 's': /* Use "safe, slow and stupid" code */
512 stupid = 1;
513 break;
514 case 'r': /* RAID mode */
515 raid_mode = 1;
516 break;
517 case 'f': /* Force install */
518 force = 1;
519 break;
520 case 'm': /* Write MBR */
521 writembr = 1;
522 break;
523 case 'a': /* Set partition active */
524 set_active = 1;
525 break;
526 case 'd':
527 if ( argp[1] )
528 subdir = *++argp;
529 break;
530 default:
531 usage();
533 opt++;
535 } else {
536 if ( bootsecfile )
537 usage();
538 else if ( device )
539 bootsecfile = *argp;
540 else
541 device = *argp;
545 if ( !device )
546 usage();
549 * Figure out which drive we're talking to
551 dev_fd = (device[0] & ~0x20) - 0x40;
552 if ( dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2] )
553 usage();
555 set_lock_device(dev_fd);
557 lock_device(2); /* Make sure we can lock the device */
558 read_device(dev_fd, sectbuf, 1, 0);
559 unlock_device(1);
562 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
564 if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
565 unlock_device(0);
566 puts(errmsg);
567 putchar('\n');
568 exit(1);
571 ldlinux_name[0] = dev_fd | 0x40;
573 set_attributes(ldlinux_name, 0);
574 fd = creat(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
575 write_file(fd, syslinux_ldlinux, syslinux_ldlinux_len);
576 close(fd);
579 * Now, use libfat to create a block map. This probably
580 * should be changed to use ioctl(...,FIBMAP,...) since
581 * this is supposed to be a simple, privileged version
582 * of the installer.
584 lock_device(2);
585 fs = libfat_open(libfat_xpread, dev_fd);
586 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
587 secp = sectors;
588 nsectors = 0;
589 s = libfat_clustertosector(fs, ldlinux_cluster);
590 while ( s && nsectors < 65 ) {
591 *secp++ = s;
592 nsectors++;
593 s = libfat_nextsector(fs, s);
595 libfat_close(fs);
598 * If requested, move ldlinux.sys
600 if (subdir) {
601 char new_ldlinux_name[160];
602 char *cp = new_ldlinux_name+3;
603 const char *sd;
604 int slash = 1;
606 new_ldlinux_name[0] = dev_fd | 0x40;
607 new_ldlinux_name[1] = ':';
608 new_ldlinux_name[2] = '\\';
610 for (sd = subdir; *sd; sd++) {
611 char c = *sd;
613 if (c == '/' || c == '\\') {
614 if (slash)
615 continue;
616 c = '\\';
617 slash = 1;
618 } else {
619 slash = 0;
622 *cp++ = c;
625 /* Skip if subdirectory == root */
626 if (cp > new_ldlinux_name+3) {
627 if (!slash)
628 *cp++ = '\\';
630 memcpy(cp, "ldlinux.sys", 12);
632 set_attributes(ldlinux_name, 0);
633 if (rename(ldlinux_name, new_ldlinux_name))
634 set_attributes(ldlinux_name, 0x07);
635 else
636 set_attributes(new_ldlinux_name, 0x07);
641 * Patch ldlinux.sys and the boot sector
643 syslinux_patch(sectors, nsectors, stupid, raid_mode);
646 * Write the now-patched first sector of ldlinux.sys
648 lock_device(3);
649 write_device(dev_fd, syslinux_ldlinux, 1, sectors[0]);
652 * Muck with the MBR, if desired, while we hold the lock
654 adjust_mbr(dev_fd, writembr, set_active);
657 * To finish up, write the boot sector
660 /* Read the superblock again since it might have changed while mounted */
661 read_device(dev_fd, sectbuf, 1, 0);
663 /* Copy the syslinux code into the boot sector */
664 syslinux_make_bootsect(sectbuf);
666 /* Write new boot sector */
667 if ( bootsecfile ) {
668 unlock_device(0);
669 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
670 write_file(fd, sectbuf, 512);
671 close(fd);
672 } else {
673 write_device(dev_fd, sectbuf, 1, 0);
674 unlock_device(0);
677 /* Done! */
679 return 0;