Releasing debian version 4.04+dfsg-9.
[syslinux-debian/hramrach.git] / dos / syslinux.c
blobb5fdfc5266f7d95334762a5982035672456323ab
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 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 <getopt.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include "mystuff.h"
28 #include "syslinux.h"
29 #include "libfat.h"
30 #include "setadv.h"
31 #include "sysexits.h"
32 #include "syslxopt.h"
33 #include "syslxint.h"
35 char *program = "syslinux.com"; /* Name of program */
36 uint16_t dos_version;
38 #ifdef DEBUG
39 # define dprintf printf
40 void pause(void)
42 uint16_t ax;
44 asm volatile("int $0x16" : "=a" (ax) : "a" (0));
46 #else
47 # define dprintf(...) ((void)0)
48 # define pause() ((void)0)
49 #endif
51 void unlock_device(int);
53 void __attribute__ ((noreturn)) die(const char *msg)
55 unlock_device(0);
56 puts("syslinux: ");
57 puts(msg);
58 putchar('\n');
59 exit(1);
62 void warning(const char *msg)
64 puts("syslinux: warning: ");
65 puts(msg);
66 putchar('\n');
70 * read/write wrapper functions
72 int creat(const char *filename, int mode)
74 uint16_t rv;
75 uint8_t err;
77 dprintf("creat(\"%s\", 0x%x)\n", filename, mode);
79 rv = 0x3C00;
80 asm volatile ("int $0x21 ; setc %0"
81 : "=bcdm" (err), "+a" (rv)
82 : "c" (mode), "d" (filename));
83 if (err) {
84 dprintf("rv = %d\n", rv);
85 die("cannot open ldlinux.sys");
88 return rv;
91 void close(int fd)
93 uint16_t rv = 0x3E00;
95 dprintf("close(%d)\n", fd);
97 asm volatile ("int $0x21":"+a" (rv)
98 :"b"(fd));
100 /* The only error MS-DOS returns for close is EBADF,
101 and we really don't care... */
104 int rename(const char *oldname, const char *newname)
106 uint16_t rv = 0x5600; /* Also support 43FFh? */
107 uint8_t err;
109 dprintf("rename(\"%s\", \"%s\")\n", oldname, newname);
111 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv)
112 :"d"(oldname), "D"(newname));
114 if (err) {
115 dprintf("rv = %d\n", rv);
116 warning("cannot move ldlinux.sys");
117 return rv;
120 return 0;
123 ssize_t write_ldlinux(int fd)
125 uint16_t ldlinux_seg = ((size_t)syslinux_ldlinux >> 4) + ds();
126 uint32_t offset = 0;
127 uint16_t rv;
128 uint8_t err;
130 while (offset < syslinux_ldlinux_len) {
131 uint32_t chunk = syslinux_ldlinux_len - offset;
132 if (chunk > 32768)
133 chunk = 32768;
134 asm volatile ("pushw %%ds ; "
135 "movw %6,%%ds ; "
136 "int $0x21 ; "
137 "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv)
138 :"a"(0x4000), "b"(fd), "c"(chunk), "d" (offset & 15),
139 "SD" ((uint16_t)(ldlinux_seg + (offset >> 4))));
140 if (err || rv == 0)
141 die("file write error");
142 offset += rv;
145 return offset;
148 ssize_t write_file(int fd, const void *buf, size_t count)
150 uint16_t rv;
151 ssize_t done = 0;
152 uint8_t err;
154 dprintf("write_file(%d,%p,%u)\n", fd, buf, count);
156 while (count) {
157 asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv)
158 :"a"(0x4000), "b"(fd), "c"(count), "d"(buf));
159 if (err || rv == 0)
160 die("file write error");
162 done += rv;
163 count -= rv;
166 return done;
169 static inline __attribute__ ((const))
170 uint16_t data_segment(void)
172 uint16_t ds;
174 asm("movw %%ds,%0" : "=rm"(ds));
175 return ds;
178 void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector)
180 uint16_t errnum = 0x0001;
181 struct diskio dio;
183 dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector);
185 dio.startsector = sector;
186 dio.sectors = nsecs;
187 dio.bufoffs = (uintptr_t) buf;
188 dio.bufseg = data_segment();
190 if (dos_version >= 0x070a) {
191 /* Try FAT32-aware system call first */
192 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
193 "1:"
194 : "=a" (errnum)
195 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
196 "S" (1), "m" (dio)
197 : "memory");
198 dprintf(" rv(7305) = %04x", errnum);
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, void *buf, size_t nsecs, unsigned int sector)
213 uint16_t errnum = 0x0001;
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 if (dos_version >= 0x070a) {
224 /* Try FAT32-aware system call first */
225 asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n"
226 "1:"
227 : "=a" (errnum)
228 : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive),
229 "S" (0), "m" (dio));
230 dprintf(" rv(7305) = %04x", errnum);
233 /* If not supported, try the legacy system call (int2526.S) */
234 if (errnum == 0x0001)
235 errnum = int25_read_sector(drive, &dio);
237 if (errnum) {
238 dprintf("rv = %04x\n", errnum);
239 die("sector read error");
243 /* Both traditional DOS and FAT32 DOS return this structure, but
244 FAT32 return a lot more data, so make sure we have plenty of space */
245 struct deviceparams {
246 uint8_t specfunc;
247 uint8_t devtype;
248 uint16_t devattr;
249 uint16_t cylinders;
250 uint8_t mediatype;
251 uint16_t bytespersec;
252 uint8_t secperclust;
253 uint16_t ressectors;
254 uint8_t fats;
255 uint16_t rootdirents;
256 uint16_t sectors;
257 uint8_t media;
258 uint16_t fatsecs;
259 uint16_t secpertrack;
260 uint16_t heads;
261 uint32_t hiddensecs;
262 uint32_t hugesectors;
263 uint8_t lotsofpadding[224];
264 } __attribute__ ((packed));
266 uint32_t get_partition_offset(int drive)
268 uint8_t err;
269 uint16_t rv;
270 struct deviceparams dp;
272 dp.specfunc = 1; /* Get current information */
274 rv = 0x440d;
275 asm volatile ("int $0x21 ; setc %0"
276 :"=abcdm" (err), "+a"(rv), "=m"(dp)
277 :"b" (drive), "c" (0x0860), "d" (&dp));
279 if (!err)
280 return dp.hiddensecs;
282 rv = 0x440d;
283 asm volatile ("int $0x21 ; setc %0"
284 : "=abcdm" (err), "+a" (rv), "=m" (dp)
285 : "b" (drive), "c" (0x4860), "d" (&dp));
287 if (!err)
288 return dp.hiddensecs;
290 die("could not find partition start offset");
293 struct rwblock {
294 uint8_t special;
295 uint16_t head;
296 uint16_t cylinder;
297 uint16_t firstsector;
298 uint16_t sectors;
299 uint16_t bufferoffset;
300 uint16_t bufferseg;
301 } __attribute__ ((packed));
303 static struct rwblock mbr = {
304 .special = 0,
305 .head = 0,
306 .cylinder = 0,
307 .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */
308 .sectors = 1,
309 .bufferoffset = 0,
310 .bufferseg = 0
313 void write_mbr(int drive, const void *buf)
315 uint16_t rv;
316 uint8_t err;
318 dprintf("write_mbr(%d,%p)", drive, buf);
320 mbr.bufferoffset = (uintptr_t) buf;
321 mbr.bufferseg = data_segment();
323 rv = 0x440d;
324 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
325 :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr));
327 dprintf(" rv(0841) = %04x", rv);
328 if (!err) {
329 dprintf("\n");
330 return;
333 rv = 0x440d;
334 asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv)
335 :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr));
337 dprintf(" rv(4841) = %04x\n", rv);
338 if (err)
339 die("mbr write error");
342 void read_mbr(int drive, const void *buf)
344 uint16_t rv;
345 uint8_t err;
347 dprintf("read_mbr(%d,%p)", drive, buf);
349 mbr.bufferoffset = (uintptr_t) buf;
350 mbr.bufferseg = data_segment();
352 rv = 0x440d;
353 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
354 :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr));
356 dprintf(" rv(0861) = %04x", rv);
357 if (!err) {
358 dprintf("\n");
359 return;
362 rv = 0x440d;
363 asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv)
364 :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr));
366 dprintf(" rv(4841) = %04x\n", rv);
367 if (err)
368 die("mbr read error");
370 dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
371 ((const uint8_t *)buf)[0],
372 ((const uint8_t *)buf)[1],
373 ((const uint8_t *)buf)[2],
374 ((const uint8_t *)buf)[3],
375 ((const uint8_t *)buf)[4],
376 ((const uint8_t *)buf)[5],
377 ((const uint8_t *)buf)[6],
378 ((const uint8_t *)buf)[7]);
381 /* This call can legitimately fail, and we don't care, so ignore error return */
382 void set_attributes(const char *file, int attributes)
384 uint16_t rv = 0x4301;
386 dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes);
388 asm volatile ("int $0x21":"+a" (rv)
389 :"c"(attributes), "d"(file));
393 * Version of the read_device function suitable for libfat
395 int libfat_xpread(intptr_t pp, void *buf, size_t secsize,
396 libfat_sector_t sector)
398 read_device(pp, buf, 1, sector);
399 return secsize;
402 static inline void get_dos_version(void)
404 uint16_t ver;
406 asm("int $0x21 ; xchgb %%ah,%%al"
407 : "=a" (ver)
408 : "a" (0x3001)
409 : "ebx", "ecx");
410 dos_version = ver;
412 dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff);
415 /* The locking interface relies on static variables. A massive hack :( */
416 static uint8_t lock_level, lock_drive;
418 static inline void set_lock_device(uint8_t device)
420 lock_level = 0;
421 lock_drive = device;
424 static int do_lock(uint8_t level)
426 uint16_t level_arg = lock_drive + (level << 8);
427 uint16_t rv;
428 uint8_t err;
429 #if 0
430 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
431 uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A;
432 #else
433 uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */
434 #endif
436 dprintf("Trying lock %04x... ", level_arg);
437 asm volatile ("int $0x21 ; setc %0"
438 : "=bcdm" (err), "=a" (rv)
439 : "a" (0x440d), "b" (level_arg),
440 "c" (lock_call), "d" (0x0001));
441 dprintf("%s %04x\n", err ? "err" : "ok", rv);
443 return err ? rv : 0;
446 void lock_device(int level)
448 static int hard_lock = 0;
449 int err;
451 if (dos_version < 0x0700)
452 return; /* Win9x/NT only */
454 if (!hard_lock) {
455 /* Assume hierarchial "soft" locking supported */
457 while (lock_level < level) {
458 int new_level = lock_level + 1;
459 err = do_lock(new_level);
460 if (err) {
461 if (err == 0x0001) {
462 /* Try hard locking next */
463 hard_lock = 1;
465 goto soft_fail;
468 lock_level = new_level;
470 return;
473 soft_fail:
474 if (hard_lock) {
475 /* Hard locking, only level 4 supported */
476 /* This is needed for Win9x in DOS mode */
478 err = do_lock(4);
479 if (err) {
480 if (err == 0x0001) {
481 /* Assume locking is not needed */
482 return;
484 goto hard_fail;
487 lock_level = 4;
488 return;
491 hard_fail:
492 die("could not lock device");
495 void unlock_device(int level)
497 uint16_t rv;
498 uint8_t err;
499 uint16_t unlock_call;
501 if (dos_version < 0x0700)
502 return; /* Win9x/NT only */
504 #if 0
505 /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */
506 unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A;
507 #else
508 unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */
509 #endif
511 if (lock_level == 4 && level > 0)
512 return; /* Only drop the hard lock at the end */
514 while (lock_level > level) {
515 uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1;
516 uint16_t level_arg = (new_level << 8) + lock_drive;
517 rv = 0x440d;
518 dprintf("Trying unlock %04x... ", new_level);
519 asm volatile ("int $0x21 ; setc %0"
520 : "=bcdm" (err), "+a" (rv)
521 : "b" (level_arg), "c" (unlock_call));
522 dprintf("%s %04x\n", err ? "err" : "ok", rv);
523 lock_level = new_level;
528 * This function does any desired MBR manipulation; called with the device lock held.
530 struct mbr_entry {
531 uint8_t active; /* Active flag */
532 uint8_t bhead; /* Begin head */
533 uint8_t bsector; /* Begin sector */
534 uint8_t bcylinder; /* Begin cylinder */
535 uint8_t filesystem; /* Filesystem value */
536 uint8_t ehead; /* End head */
537 uint8_t esector; /* End sector */
538 uint8_t ecylinder; /* End cylinder */
539 uint32_t startlba; /* Start sector LBA */
540 uint32_t sectors; /* Length in sectors */
541 } __attribute__ ((packed));
543 static void adjust_mbr(int device, int writembr, int set_active)
545 static unsigned char sectbuf[SECTOR_SIZE];
546 int i;
548 if (!writembr && !set_active)
549 return; /* Nothing to do */
551 read_mbr(device, sectbuf);
553 if (writembr) {
554 memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len);
555 *(uint16_t *) (sectbuf + 510) = 0xaa55;
558 if (set_active) {
559 uint32_t offset = get_partition_offset(device);
560 struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446);
561 int found = 0;
563 dprintf("Searching for partition offset: %08x\n", offset);
565 for (i = 0; i < 4; i++) {
566 if (me->startlba == offset) {
567 me->active = 0x80;
568 found++;
569 } else {
570 me->active = 0;
572 me++;
575 if (found < 1) {
576 die("partition not found (-a is not implemented for logical partitions)");
577 } else if (found > 1) {
578 die("multiple aliased partitions found");
582 write_mbr(device, sectbuf);
585 int main(int argc, char *argv[])
587 static unsigned char sectbuf[SECTOR_SIZE];
588 int dev_fd, fd;
589 static char ldlinux_name[] = "@:\\ldlinux.sys";
590 struct libfat_filesystem *fs;
591 libfat_sector_t s, *secp;
592 libfat_sector_t *sectors;
593 int ldlinux_sectors;
594 int32_t ldlinux_cluster;
595 int nsectors;
596 const char *errmsg;
597 int i;
598 int patch_sectors;
599 unsigned char *dp;
601 dprintf("argv = %p\n", argv);
602 for (i = 0; i <= argc; i++)
603 dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]);
605 get_dos_version();
607 argv[0] = program;
608 parse_options(argc, argv, MODE_SYSLINUX_DOSWIN);
610 if (!opt.device)
611 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
612 if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once
613 || (opt.update_only > 0) || opt.menu_save || opt.offset) {
614 fprintf(stderr,
615 "At least one specified option not yet implemented"
616 " for this installer.\n");
617 exit(1);
621 * Create an ADV in memory... this should be smarter.
623 syslinux_reset_adv(syslinux_adv);
626 * Figure out which drive we're talking to
628 dev_fd = (opt.device[0] & ~0x20) - 0x40;
629 if (dev_fd < 1 || dev_fd > 26 || opt.device[1] != ':' || opt.device[2])
630 usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
632 set_lock_device(dev_fd);
634 lock_device(2); /* Make sure we can lock the device */
635 read_device(dev_fd, sectbuf, 1, 0);
636 unlock_device(1);
639 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
641 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
642 unlock_device(0);
643 puts(errmsg);
644 putchar('\n');
645 exit(1);
648 ldlinux_name[0] = dev_fd | 0x40;
650 set_attributes(ldlinux_name, 0);
651 fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */
652 write_ldlinux(fd);
653 write_file(fd, syslinux_adv, 2 * ADV_SIZE);
654 close(fd);
655 set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */
658 * Now, use libfat to create a block map. This probably
659 * should be changed to use ioctl(...,FIBMAP,...) since
660 * this is supposed to be a simple, privileged version
661 * of the installer.
663 ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE
664 + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
665 sectors = calloc(ldlinux_sectors, sizeof *sectors);
666 lock_device(2);
667 fs = libfat_open(libfat_xpread, dev_fd);
668 ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
669 secp = sectors;
670 nsectors = 0;
671 s = libfat_clustertosector(fs, ldlinux_cluster);
672 while (s && nsectors < ldlinux_sectors) {
673 *secp++ = s;
674 nsectors++;
675 s = libfat_nextsector(fs, s);
677 libfat_close(fs);
680 * If requested, move ldlinux.sys
682 if (opt.directory) {
683 char new_ldlinux_name[160];
684 char *cp = new_ldlinux_name + 3;
685 const char *sd;
686 int slash = 1;
688 new_ldlinux_name[0] = dev_fd | 0x40;
689 new_ldlinux_name[1] = ':';
690 new_ldlinux_name[2] = '\\';
692 for (sd = opt.directory; *sd; sd++) {
693 char c = *sd;
695 if (c == '/' || c == '\\') {
696 if (slash)
697 continue;
698 c = '\\';
699 slash = 1;
700 } else {
701 slash = 0;
704 *cp++ = c;
707 /* Skip if subdirectory == root */
708 if (cp > new_ldlinux_name + 3) {
709 if (!slash)
710 *cp++ = '\\';
712 memcpy(cp, "ldlinux.sys", 12);
714 set_attributes(ldlinux_name, 0);
715 if (rename(ldlinux_name, new_ldlinux_name))
716 set_attributes(ldlinux_name, 0x07);
717 else
718 set_attributes(new_ldlinux_name, 0x07);
723 * Patch ldlinux.sys and the boot sector
725 i = syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode, opt.directory, NULL);
726 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
729 * Overwrite the now-patched ldlinux.sys
731 /* lock_device(3); -- doesn't seem to be needed */
732 dp = syslinux_ldlinux;
733 for (i = 0; i < patch_sectors; i++) {
734 memcpy_from_sl(sectbuf, dp, SECTOR_SIZE);
735 dp += SECTOR_SIZE;
736 write_device(dev_fd, sectbuf, 1, sectors[i]);
740 * Muck with the MBR, if desired, while we hold the lock
742 adjust_mbr(dev_fd, opt.install_mbr, opt.activate_partition);
745 * To finish up, write the boot sector
748 /* Read the superblock again since it might have changed while mounted */
749 read_device(dev_fd, sectbuf, 1, 0);
751 /* Copy the syslinux code into the boot sector */
752 syslinux_make_bootsect(sectbuf);
754 /* Write new boot sector */
755 if (opt.bootsecfile) {
756 unlock_device(0);
757 fd = creat(opt.bootsecfile, 0x20); /* ARCHIVE */
758 write_file(fd, sectbuf, SECTOR_SIZE);
759 close(fd);
760 } else {
761 write_device(dev_fd, sectbuf, 1, 0);
762 unlock_device(0);
765 /* Done! */
767 return 0;