Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / dos / syslinux.c
blob5dc34836b26f70e9a1fbcb9bd9effbc061655012
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
15 * syslinux.c - Linux installer program for SYSLINUX
17 * Hacked up for DOS.
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include "mystuff.h"
27 #include "syslinux.h"
28 #include "libfat.h"
30 const char *program = "syslinux"; /* Name of program */
31 uint16_t dos_version;
33 #ifdef DEBUG
34 # define dprintf printf
35 void pause(void)
37 uint16_t ax;
39 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
41 #else
42 # define dprintf(...) ((void)0)
43 # define pause() ((void)0)
44 #endif
46 void __attribute__ ((noreturn)) usage(void)
48 puts("Usage: syslinux [-sfmar][-d directory] <drive>: [bootsecfile]\n");
49 exit(1);
52 void unlock_device(int);
54 void __attribute__ ((noreturn)) die(const char *msg)
56 unlock_device(0);
57 puts("syslinux: ");
58 puts(msg);
59 putchar('\n');
60 exit(1);
63 void warning(const char *msg)
65 puts("syslinux: warning: ");
66 puts(msg);
67 putchar('\n');
71 * read/write wrapper functions
73 int creat(const char *filename, int mode)
75 uint16_t rv;
76 uint8_t err;
78 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
80 rv = 0x3C00;
81 asm volatile ("int $0x21 ; setc %0"
82 : "=bcdm" (err), "+a" (rv)
83 : "c" (mode), "d" (filename));
84 if (err) {
85 dprintf("rv = %d\n", rv);
86 die("cannot open ldlinux.sys");
89 return rv;
92 void close(int fd)
94 uint16_t rv = 0x3E00;
96 dprintf("close(%d)\n", fd);
98 asm volatile ("int $0x21":"+a" (rv)
99 :"b"(fd));
101 /* The only error MS-DOS returns for close is EBADF,
102 and we really don't care... */
105 int rename(const char *oldname, const char *newname)
107 uint16_t rv = 0x5600; /* Also support 43FFh? */
108 uint8_t err;
110 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
112 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
113 :"d"(oldname), "D"(newname));
115 if (err) {
116 dprintf("rv = %d\n", rv);
117 warning("cannot move ldlinux.sys");
118 return rv;
121 return 0;
124 extern const char __payload_sseg[];
125 uint16_t ldlinux_seg;
127 ssize_t write_ldlinux(int fd)
129 uint32_t offset = 0;
130 uint16_t rv;
131 uint8_t err;
133 while (offset < syslinux_ldlinux_len) {
134 uint32_t chunk = syslinux_ldlinux_len - offset;
135 if (chunk > 32768)
136 chunk = 32768;
137 asm volatile ("pushw %%ds ; "
138 "movw %6,%%ds ; "
139 "int $0x21 ; "
140 "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv)
141 :"a"(0x4000), "b"(fd), "c"(chunk), "d"(offset & 15),
142 "SD"((uint16_t) (ldlinux_seg + (offset >> 4))));
143 if (err || rv == 0)
144 die("file write error");
145 offset += rv;
148 return offset;
151 ssize_t write_file(int fd, const void *buf, size_t count)
153 uint16_t rv;
154 ssize_t done = 0;
155 uint8_t err;
157 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
159 while (count) {
160 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
161 :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
162 if (err || rv == 0)
163 die("file write error");
165 done += rv;
166 count -= rv;
169 return done;
172 static inline __attribute__ ((const))
173 uint16_t data_segment(void)
175 uint16_t ds;
177 asm("movw %%ds,%0" : "=rm"(ds));
178 return ds;
181 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
183 uint16_t errnum;
184 struct diskio dio;
186 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
188 dio.startsector = sector;
189 dio.sectors = nsecs;
190 dio.bufoffs = (uintptr_t) buf;
191 dio.bufseg = data_segment();
193 /* Try FAT32-aware system call first */
194 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
195 "1:"
196 : "=a" (errnum)
197 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
198 "S" (1), "m" (dio)
199 : "memory");
201 /* If not supported, try the legacy system call (int2526.S) */
202 if (errnum == 0x0001)
203 errnum = int26_write_sector(drive, &dio);
205 if (errnum) {
206 dprintf("rv = %04x\n", errnum);
207 die("sector write error");
211 void read_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
213 uint16_t errnum;
214 struct diskio dio;
216 dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
218 dio.startsector = sector;
219 dio.sectors = nsecs;
220 dio.bufoffs = (uintptr_t) buf;
221 dio.bufseg = data_segment();
223 /* Try FAT32-aware system call first */
224 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
225 "1:"
226 : "=a" (errnum)
227 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
228 "S" (0), "m" (dio));
230 /* If not supported, try the legacy system call (int2526.S) */
231 if (errnum == 0x0001)
232 errnum = int25_read_sector(drive, &dio);
234 if (errnum) {
235 dprintf("rv = %04x\n", errnum);
236 die("sector read error");
240 /* Both traditional DOS and FAT32 DOS return this structure, but
241 FAT32 return a lot more data, so make sure we have plenty of space */
242 struct deviceparams {
243 uint8_t specfunc;
244 uint8_t devtype;
245 uint16_t devattr;
246 uint16_t cylinders;
247 uint8_t mediatype;
248 uint16_t bytespersec;
249 uint8_t secperclust;
250 uint16_t ressectors;
251 uint8_t fats;
252 uint16_t rootdirents;
253 uint16_t sectors;
254 uint8_t media;
255 uint16_t fatsecs;
256 uint16_t secpertrack;
257 uint16_t heads;
258 uint32_t hiddensecs;
259 uint32_t hugesectors;
260 uint8_t lotsofpadding[224];
261 } __attribute__ ((packed));
263 uint32_t get_partition_offset(int drive)
265 uint8_t err;
266 uint16_t rv;
267 struct deviceparams dp;
269 dp.specfunc = 1; /* Get current information */
271 rv = 0x440d;
272 asm volatile ("int $0x21 ; setc %0"
273 :"=abcdm" (err), "+a"(rv), "=m"(dp)
274 :"b" (drive), "c" (0x0860), "d" (&dp));
276 if (!err)
277 return dp.hiddensecs;
279 rv = 0x440d;
280 asm volatile ("int $0x21 ; setc %0"
281 : "=abcdm" (err), "+a" (rv), "=m" (dp)
282 : "b" (drive), "c" (0x4860), "d" (&dp));
284 if (!err)
285 return dp.hiddensecs;
287 die("could not find partition start offset");
290 struct rwblock {
291 uint8_t special;
292 uint16_t head;
293 uint16_t cylinder;
294 uint16_t firstsector;
295 uint16_t sectors;
296 uint16_t bufferoffset;
297 uint16_t bufferseg;
298 } __attribute__ ((packed));
300 static struct rwblock mbr = {
301 .special = 0,
302 .head = 0,
303 .cylinder = 0,
304 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
305 .sectors = 1,
306 .bufferoffset = 0,
307 .bufferseg = 0
310 void write_mbr(int drive, const void *buf)
312 uint16_t rv;
313 uint8_t err;
315 dprintf("write_mbr(%d,%p)", drive, buf);
317 mbr.bufferoffset = (uintptr_t) buf;
318 mbr.bufferseg = data_segment();
320 rv = 0x440d;
321 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
322 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
324 dprintf(" rv(0841) = %04x", rv);
325 if (!err) {
326 dprintf("\n");
327 return;
330 rv = 0x440d;
331 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
332 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
334 dprintf(" rv(4841) = %04x\n", rv);
335 if (err)
336 die("mbr write error");
339 void read_mbr(int drive, const void *buf)
341 uint16_t rv;
342 uint8_t err;
344 dprintf("read_mbr(%d,%p)", drive, buf);
346 mbr.bufferoffset = (uintptr_t) buf;
347 mbr.bufferseg = data_segment();
349 rv = 0x440d;
350 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
351 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
353 dprintf(" rv(0861) = %04x", rv);
354 if (!err) {
355 dprintf("\n");
356 return;
359 rv = 0x440d;
360 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
361 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
363 dprintf(" rv(4841) = %04x\n", rv);
364 if (err)
365 die("mbr read error");
367 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
368 ((const uint8_t *)buf)[0],
369 ((const uint8_t *)buf)[1],
370 ((const uint8_t *)buf)[2],
371 ((const uint8_t *)buf)[3],
372 ((const uint8_t *)buf)[4],
373 ((const uint8_t *)buf)[5],
374 ((const uint8_t *)buf)[6],
375 ((const uint8_t *)buf)[7]);
378 /* This call can legitimately fail, and we don't care, so ignore error return */
379 void set_attributes(const char *file, int attributes)
381 uint16_t rv = 0x4301;
383 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
385 asm volatile ("int $0x21":"+a" (rv)
386 :"c"(attributes), "d"(file));
390 * Version of the read_device function suitable for libfat
392 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
393 libfat_sector_t sector)
395 read_device(pp, buf, 1, sector);
396 return secsize;
399 static inline void get_dos_version(void)
401 uint16_t ver;
403 asm("int $0x21 ; xchgb %%ah,%%al"
404 : "=a" (ver)
405 : "a" (0x3001)
406 : "ebx", "ecx");
407 dos_version = ver;
409 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
412 /* The locking interface relies on static variables. A massive hack :( */
413 static uint8_t lock_level, lock_drive;
415 static inline void set_lock_device(uint8_t device)
417 lock_level = 0;
418 lock_drive = device;
421 static int do_lock(uint8_t level)
423 uint16_t level_arg = lock_drive + (level << 8);
424 uint16_t rv;
425 uint8_t err;
426 #if 0
427 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
428 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
429 #else
430 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
431 #endif
433 dprintf("Trying lock %04x... ", level_arg);
434 asm volatile ("int $0x21 ; setc %0"
435 : "=bcdm" (err), "=a" (rv)
436 : "a" (0x440d), "b" (level_arg),
437 "c" (lock_call), "d" (0x0001));
438 dprintf("%s %04x\n", err ? "err" : "ok", rv);
440 return err ? rv : 0;
443 void lock_device(int level)
445 static int hard_lock = 0;
446 int err;
448 if (dos_version < 0x0700)
449 return; /* Win9x/NT only */
451 if (!hard_lock) {
452 /* Assume hierarchial "soft" locking supported */
454 while (lock_level < level) {
455 int new_level = lock_level + 1;
456 err = do_lock(new_level);
457 if (err) {
458 if (err == 0x0001) {
459 /* Try hard locking next */
460 hard_lock = 1;
462 goto soft_fail;
465 lock_level = new_level;
467 return;
470 soft_fail:
471 if (hard_lock) {
472 /* Hard locking, only level 4 supported */
473 /* This is needed for Win9x in DOS mode */
475 err = do_lock(4);
476 if (err) {
477 if (err == 0x0001) {
478 /* Assume locking is not needed */
479 return;
481 goto hard_fail;
484 lock_level = 4;
485 return;
488 hard_fail:
489 die("could not lock device");
492 void unlock_device(int level)
494 uint16_t rv;
495 uint8_t err;
496 uint16_t unlock_call;
498 if (dos_version < 0x0700)
499 return; /* Win9x/NT only */
501 #if 0
502 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
503 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
504 #else
505 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
506 #endif
508 if (lock_level == 4 && level > 0)
509 return; /* Only drop the hard lock at the end */
511 while (lock_level > level) {
512 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
513 uint16_t level_arg = (new_level << 8) + lock_drive;
514 rv = 0x440d;
515 dprintf("Trying unlock %04x... ", new_level);
516 asm volatile ("int $0x21 ; setc %0"
517 : "=bcdm" (err), "+a" (rv)
518 : "b" (level_arg), "c" (unlock_call));
519 dprintf("%s %04x\n", err ? "err" : "ok", rv);
520 lock_level = new_level;
525 * This function does any desired MBR manipulation; called with the device lock held.
527 struct mbr_entry {
528 uint8_t active; /* Active flag */
529 uint8_t bhead; /* Begin head */
530 uint8_t bsector; /* Begin sector */
531 uint8_t bcylinder; /* Begin cylinder */
532 uint8_t filesystem; /* Filesystem value */
533 uint8_t ehead; /* End head */
534 uint8_t esector; /* End sector */
535 uint8_t ecylinder; /* End cylinder */
536 uint32_t startlba; /* Start sector LBA */
537 uint32_t sectors; /* Length in sectors */
538 } __attribute__ ((packed));
540 static void adjust_mbr(int device, int writembr, int set_active)
542 static unsigned char sectbuf[SECTOR_SIZE];
543 int i;
545 if (!writembr && !set_active)
546 return; /* Nothing to do */
548 read_mbr(device, sectbuf);
550 if (writembr) {
551 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
552 *(uint16_t *) (sectbuf + 510) = 0xaa55;
555 if (set_active) {
556 uint32_t offset = get_partition_offset(device);
557 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
558 int found = 0;
560 dprintf("Searching for partition offset: %08x\n", offset);
562 for (i = 0; i < 4; i++) {
563 if (me->startlba == offset) {
564 me->active = 0x80;
565 found++;
566 } else {
567 me->active = 0;
569 me++;
572 if (found < 1) {
573 die("partition not found (-a is not implemented for logical partitions)");
574 } else if (found > 1) {
575 die("multiple aliased partitions found");
579 write_mbr(device, sectbuf);
582 int main(int argc, char *argv[])
584 static unsigned char sectbuf[SECTOR_SIZE];
585 int dev_fd, fd;
586 static char ldlinux_name[] = "@:\\ldlinux.sys";
587 char **argp, *opt;
588 int force = 0; /* -f (force) option */
589 struct libfat_filesystem *fs;
590 libfat_sector_t s, *secp;
591 libfat_sector_t *sectors;
592 int ldlinux_sectors;
593 int32_t ldlinux_cluster;
594 int nsectors;
595 const char *device = NULL, *bootsecfile = NULL;
596 const char *errmsg;
597 int i;
598 int writembr = 0; /* -m (write MBR) option */
599 int set_active = 0; /* -a (set partition active) option */
600 const char *subdir = NULL;
601 int stupid = 0;
602 int raid_mode = 0;
603 int patch_sectors;
605 ldlinux_seg = (size_t) __payload_sseg + data_segment();
607 dprintf("argv = %p\n", argv);
608 for (i = 0; i <= argc; i++)
609 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
611 (void)argc; /* Unused */
613 get_dos_version();
615 for (argp = argv + 1; *argp; argp++) {
616 if (**argp == '-') {
617 opt = *argp + 1;
618 if (!*opt)
619 usage();
621 while (*opt) {
622 switch (*opt) {
623 case 's': /* Use "safe, slow and stupid" code */
624 stupid = 1;
625 break;
626 case 'r': /* RAID mode */
627 raid_mode = 1;
628 break;
629 case 'f': /* Force install */
630 force = 1;
631 break;
632 case 'm': /* Write MBR */
633 writembr = 1;
634 break;
635 case 'a': /* Set partition active */
636 set_active = 1;
637 break;
638 case 'd':
639 if (argp[1])
640 subdir = *++argp;
641 break;
642 default:
643 usage();
645 opt++;
647 } else {
648 if (bootsecfile)
649 usage();
650 else if (device)
651 bootsecfile = *argp;
652 else
653 device = *argp;
657 if (!device)
658 usage();
661 * Figure out which drive we're talking to
663 dev_fd = (device[0] & ~0x20) - 0x40;
664 if (dev_fd < 1 || dev_fd > 26 || device[1] != ':' || device[2])
665 usage();
667 set_lock_device(dev_fd);
669 lock_device(2); /* Make sure we can lock the device */
670 read_device(dev_fd, sectbuf, 1, 0);
671 unlock_device(1);
674 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
676 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
677 unlock_device(0);
678 puts(errmsg);
679 putchar('\n');
680 exit(1);
683 ldlinux_name[0] = dev_fd | 0x40;
685 set_attributes(ldlinux_name, 0);
686 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
687 write_ldlinux(fd);
688 close(fd);
689 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
692 * Now, use libfat to create a block map. This probably
693 * should be changed to use ioctl(...,FIBMAP,...) since
694 * this is supposed to be a simple, privileged version
695 * of the installer.
697 ldlinux_sectors = (syslinux_ldlinux_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
698 sectors = calloc(ldlinux_sectors, sizeof *sectors);
699 lock_device(2);
700 fs = libfat_open(libfat_xpread, dev_fd);
701 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
702 secp = sectors;
703 nsectors = 0;
704 s = libfat_clustertosector(fs, ldlinux_cluster);
705 while (s && nsectors < ldlinux_sectors) {
706 *secp++ = s;
707 nsectors++;
708 s = libfat_nextsector(fs, s);
710 libfat_close(fs);
713 * If requested, move ldlinux.sys
715 if (subdir) {
716 char new_ldlinux_name[160];
717 char *cp = new_ldlinux_name + 3;
718 const char *sd;
719 int slash = 1;
721 new_ldlinux_name[0] = dev_fd | 0x40;
722 new_ldlinux_name[1] = ':';
723 new_ldlinux_name[2] = '\\';
725 for (sd = subdir; *sd; sd++) {
726 char c = *sd;
728 if (c == '/' || c == '\\') {
729 if (slash)
730 continue;
731 c = '\\';
732 slash = 1;
733 } else {
734 slash = 0;
737 *cp++ = c;
740 /* Skip if subdirectory == root */
741 if (cp > new_ldlinux_name + 3) {
742 if (!slash)
743 *cp++ = '\\';
745 memcpy(cp, "ldlinux.sys", 12);
747 set_attributes(ldlinux_name, 0);
748 if (rename(ldlinux_name, new_ldlinux_name))
749 set_attributes(ldlinux_name, 0x07);
750 else
751 set_attributes(new_ldlinux_name, 0x07);
756 * Patch ldlinux.sys and the boot sector
758 i = syslinux_patch(sectors, nsectors, stupid, raid_mode, subdir, NULL);
759 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
762 * Overwrite the now-patched ldlinux.sys
764 /* lock_device(3); -- doesn't seem to be needed */
765 for (i = 0; i < patch_sectors; i++) {
766 uint16_t si, di, cx;
767 si = 0;
768 di = (size_t) sectbuf;
769 cx = SECTOR_SIZE >> 2;
770 asm volatile ("movw %3,%%fs ; fs ; rep ; movsl":"+S" (si), "+D"(di),
771 "+c"(cx)
772 :"abd"((uint16_t)
773 (ldlinux_seg + (i << (SECTOR_SHIFT - 4)))));
774 write_device(dev_fd, sectbuf, 1, sectors[i]);
778 * Muck with the MBR, if desired, while we hold the lock
780 adjust_mbr(dev_fd, writembr, set_active);
783 * To finish up, write the boot sector
786 /* Read the superblock again since it might have changed while mounted */
787 read_device(dev_fd, sectbuf, 1, 0);
789 /* Copy the syslinux code into the boot sector */
790 syslinux_make_bootsect(sectbuf);
792 /* Write new boot sector */
793 if (bootsecfile) {
794 unlock_device(0);
795 fd = creat(bootsecfile, 0x20); /* ARCHIVE */
796 write_file(fd, sectbuf, SECTOR_SIZE);
797 close(fd);
798 } else {
799 write_device(dev_fd, sectbuf, 1, 0);
800 unlock_device(0);
803 /* Done! */
805 return 0;