| 1 | /* ----------------------------------------------------------------------- * |
|---|
| 2 | * |
|---|
| 3 | * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved |
|---|
| 4 | * Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin |
|---|
| 5 | * |
|---|
| 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. |
|---|
| 11 | * |
|---|
| 12 | * ----------------------------------------------------------------------- */ |
|---|
| 13 | |
|---|
| 14 | /* |
|---|
| 15 | * syslinux.c - Linux installer program for SYSLINUX |
|---|
| 16 | * |
|---|
| 17 | * Hacked up for DOS. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 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" |
|---|
| 27 | |
|---|
| 28 | #include "syslinux.h" |
|---|
| 29 | #include "libfat.h" |
|---|
| 30 | #include "setadv.h" |
|---|
| 31 | #include "sysexits.h" |
|---|
| 32 | #include "syslxopt.h" |
|---|
| 33 | #include "syslxint.h" |
|---|
| 34 | #include "syslxfs.h" |
|---|
| 35 | |
|---|
| 36 | char *program = "syslinux.com"; /* Name of program */ |
|---|
| 37 | uint16_t dos_version; |
|---|
| 38 | |
|---|
| 39 | #ifdef DEBUG |
|---|
| 40 | # define dprintf printf |
|---|
| 41 | void pause(void) |
|---|
| 42 | { |
|---|
| 43 | uint16_t ax; |
|---|
| 44 | |
|---|
| 45 | asm volatile("int $0x16" : "=a" (ax) : "a" (0)); |
|---|
| 46 | } |
|---|
| 47 | #else |
|---|
| 48 | # define dprintf(...) ((void)0) |
|---|
| 49 | # define pause() ((void)0) |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | void unlock_device(int); |
|---|
| 53 | |
|---|
| 54 | void __attribute__ ((noreturn)) die(const char *msg) |
|---|
| 55 | { |
|---|
| 56 | unlock_device(0); |
|---|
| 57 | puts("syslinux: "); |
|---|
| 58 | puts(msg); |
|---|
| 59 | putchar('\n'); |
|---|
| 60 | exit(1); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void warning(const char *msg) |
|---|
| 64 | { |
|---|
| 65 | puts("syslinux: warning: "); |
|---|
| 66 | puts(msg); |
|---|
| 67 | putchar('\n'); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /* |
|---|
| 71 | * read/write wrapper functions |
|---|
| 72 | */ |
|---|
| 73 | int creat(const char *filename, int mode) |
|---|
| 74 | { |
|---|
| 75 | uint16_t rv; |
|---|
| 76 | uint8_t err; |
|---|
| 77 | |
|---|
| 78 | dprintf("creat(\"%s\", 0x%x)\n", filename, mode); |
|---|
| 79 | |
|---|
| 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"); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return rv; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void close(int fd) |
|---|
| 93 | { |
|---|
| 94 | uint16_t rv = 0x3E00; |
|---|
| 95 | |
|---|
| 96 | dprintf("close(%d)\n", fd); |
|---|
| 97 | |
|---|
| 98 | asm volatile ("int $0x21":"+a" (rv) |
|---|
| 99 | :"b"(fd)); |
|---|
| 100 | |
|---|
| 101 | /* The only error MS-DOS returns for close is EBADF, |
|---|
| 102 | and we really don't care... */ |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | int rename(const char *oldname, const char *newname) |
|---|
| 106 | { |
|---|
| 107 | uint16_t rv = 0x5600; /* Also support 43FFh? */ |
|---|
| 108 | uint8_t err; |
|---|
| 109 | |
|---|
| 110 | dprintf("rename(\"%s\", \"%s\")\n", oldname, newname); |
|---|
| 111 | |
|---|
| 112 | asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "+a"(rv) |
|---|
| 113 | :"d"(oldname), "D"(newname)); |
|---|
| 114 | |
|---|
| 115 | if (err) { |
|---|
| 116 | dprintf("rv = %d\n", rv); |
|---|
| 117 | warning("cannot move ldlinux.sys"); |
|---|
| 118 | return rv; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | return 0; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | ssize_t write_ldlinux(int fd) |
|---|
| 125 | { |
|---|
| 126 | uint16_t ldlinux_seg = ((size_t)syslinux_ldlinux >> 4) + ds(); |
|---|
| 127 | uint32_t offset = 0; |
|---|
| 128 | uint16_t rv; |
|---|
| 129 | uint8_t err; |
|---|
| 130 | |
|---|
| 131 | while (offset < syslinux_ldlinux_len) { |
|---|
| 132 | uint32_t chunk = syslinux_ldlinux_len - offset; |
|---|
| 133 | if (chunk > 32768) |
|---|
| 134 | chunk = 32768; |
|---|
| 135 | asm volatile ("pushw %%ds ; " |
|---|
| 136 | "movw %6,%%ds ; " |
|---|
| 137 | "int $0x21 ; " |
|---|
| 138 | "popw %%ds ; " "setc %0":"=bcdm" (err), "=a"(rv) |
|---|
| 139 | :"a"(0x4000), "b"(fd), "c"(chunk), "d" (offset & 15), |
|---|
| 140 | "SD" ((uint16_t)(ldlinux_seg + (offset >> 4)))); |
|---|
| 141 | if (err || rv == 0) |
|---|
| 142 | die("file write error"); |
|---|
| 143 | offset += rv; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return offset; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | ssize_t write_file(int fd, const void *buf, size_t count) |
|---|
| 150 | { |
|---|
| 151 | uint16_t rv; |
|---|
| 152 | ssize_t done = 0; |
|---|
| 153 | uint8_t err; |
|---|
| 154 | |
|---|
| 155 | dprintf("write_file(%d,%p,%u)\n", fd, buf, count); |
|---|
| 156 | |
|---|
| 157 | while (count) { |
|---|
| 158 | asm volatile ("int $0x21 ; setc %0":"=bcdm" (err), "=a"(rv) |
|---|
| 159 | :"a"(0x4000), "b"(fd), "c"(count), "d"(buf)); |
|---|
| 160 | if (err || rv == 0) |
|---|
| 161 | die("file write error"); |
|---|
| 162 | |
|---|
| 163 | done += rv; |
|---|
| 164 | count -= rv; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | return done; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | static inline __attribute__ ((const)) |
|---|
| 171 | uint16_t data_segment(void) |
|---|
| 172 | { |
|---|
| 173 | uint16_t ds; |
|---|
| 174 | |
|---|
| 175 | asm("movw %%ds,%0" : "=rm"(ds)); |
|---|
| 176 | return ds; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | void write_device(int drive, const void *buf, size_t nsecs, unsigned int sector) |
|---|
| 180 | { |
|---|
| 181 | uint16_t errnum = 0x0001; |
|---|
| 182 | struct diskio dio; |
|---|
| 183 | |
|---|
| 184 | dprintf("write_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector); |
|---|
| 185 | |
|---|
| 186 | dio.startsector = sector; |
|---|
| 187 | dio.sectors = nsecs; |
|---|
| 188 | dio.bufoffs = (uintptr_t) buf; |
|---|
| 189 | dio.bufseg = data_segment(); |
|---|
| 190 | |
|---|
| 191 | if (dos_version >= 0x070a) { |
|---|
| 192 | /* Try FAT32-aware system call first */ |
|---|
| 193 | asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n" |
|---|
| 194 | "1:" |
|---|
| 195 | : "=a" (errnum) |
|---|
| 196 | : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive), |
|---|
| 197 | "S" (1), "m" (dio) |
|---|
| 198 | : "memory"); |
|---|
| 199 | dprintf(" rv(7305) = %04x", errnum); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /* If not supported, try the legacy system call (int2526.S) */ |
|---|
| 203 | if (errnum == 0x0001) |
|---|
| 204 | errnum = int26_write_sector(drive, &dio); |
|---|
| 205 | |
|---|
| 206 | if (errnum) { |
|---|
| 207 | dprintf("rv = %04x\n", errnum); |
|---|
| 208 | die("sector write error"); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | void read_device(int drive, void *buf, size_t nsecs, unsigned int sector) |
|---|
| 213 | { |
|---|
| 214 | uint16_t errnum = 0x0001; |
|---|
| 215 | struct diskio dio; |
|---|
| 216 | |
|---|
| 217 | dprintf("read_device(%d,%p,%u,%u)\n", drive, buf, nsecs, sector); |
|---|
| 218 | |
|---|
| 219 | dio.startsector = sector; |
|---|
| 220 | dio.sectors = nsecs; |
|---|
| 221 | dio.bufoffs = (uintptr_t) buf; |
|---|
| 222 | dio.bufseg = data_segment(); |
|---|
| 223 | |
|---|
| 224 | if (dos_version >= 0x070a) { |
|---|
| 225 | /* Try FAT32-aware system call first */ |
|---|
| 226 | asm volatile("int $0x21 ; jc 1f ; xorw %0,%0\n" |
|---|
| 227 | "1:" |
|---|
| 228 | : "=a" (errnum) |
|---|
| 229 | : "a" (0x7305), "b" (&dio), "c" (-1), "d" (drive), |
|---|
| 230 | "S" (0), "m" (dio)); |
|---|
| 231 | dprintf(" rv(7305) = %04x", errnum); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /* If not supported, try the legacy system call (int2526.S) */ |
|---|
| 235 | if (errnum == 0x0001) |
|---|
| 236 | errnum = int25_read_sector(drive, &dio); |
|---|
| 237 | |
|---|
| 238 | if (errnum) { |
|---|
| 239 | dprintf("rv = %04x\n", errnum); |
|---|
| 240 | die("sector read error"); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /* Both traditional DOS and FAT32 DOS return this structure, but |
|---|
| 245 | FAT32 return a lot more data, so make sure we have plenty of space */ |
|---|
| 246 | struct deviceparams { |
|---|
| 247 | uint8_t specfunc; |
|---|
| 248 | uint8_t devtype; |
|---|
| 249 | uint16_t devattr; |
|---|
| 250 | uint16_t cylinders; |
|---|
| 251 | uint8_t mediatype; |
|---|
| 252 | uint16_t bytespersec; |
|---|
| 253 | uint8_t secperclust; |
|---|
| 254 | uint16_t ressectors; |
|---|
| 255 | uint8_t fats; |
|---|
| 256 | uint16_t rootdirents; |
|---|
| 257 | uint16_t sectors; |
|---|
| 258 | uint8_t media; |
|---|
| 259 | uint16_t fatsecs; |
|---|
| 260 | uint16_t secpertrack; |
|---|
| 261 | uint16_t heads; |
|---|
| 262 | uint32_t hiddensecs; |
|---|
| 263 | uint32_t hugesectors; |
|---|
| 264 | uint8_t lotsofpadding[224]; |
|---|
| 265 | } __attribute__ ((packed)); |
|---|
| 266 | |
|---|
| 267 | uint32_t get_partition_offset(int drive) |
|---|
| 268 | { |
|---|
| 269 | uint8_t err; |
|---|
| 270 | uint16_t rv; |
|---|
| 271 | struct deviceparams dp; |
|---|
| 272 | |
|---|
| 273 | dp.specfunc = 1; /* Get current information */ |
|---|
| 274 | |
|---|
| 275 | rv = 0x440d; |
|---|
| 276 | asm volatile ("int $0x21 ; setc %0" |
|---|
| 277 | :"=abcdm" (err), "+a"(rv), "=m"(dp) |
|---|
| 278 | :"b" (drive), "c" (0x0860), "d" (&dp)); |
|---|
| 279 | |
|---|
| 280 | if (!err) |
|---|
| 281 | return dp.hiddensecs; |
|---|
| 282 | |
|---|
| 283 | rv = 0x440d; |
|---|
| 284 | asm volatile ("int $0x21 ; setc %0" |
|---|
| 285 | : "=abcdm" (err), "+a" (rv), "=m" (dp) |
|---|
| 286 | : "b" (drive), "c" (0x4860), "d" (&dp)); |
|---|
| 287 | |
|---|
| 288 | if (!err) |
|---|
| 289 | return dp.hiddensecs; |
|---|
| 290 | |
|---|
| 291 | die("could not find partition start offset"); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | struct rwblock { |
|---|
| 295 | uint8_t special; |
|---|
| 296 | uint16_t head; |
|---|
| 297 | uint16_t cylinder; |
|---|
| 298 | uint16_t firstsector; |
|---|
| 299 | uint16_t sectors; |
|---|
| 300 | uint16_t bufferoffset; |
|---|
| 301 | uint16_t bufferseg; |
|---|
| 302 | } __attribute__ ((packed)); |
|---|
| 303 | |
|---|
| 304 | static struct rwblock mbr = { |
|---|
| 305 | .special = 0, |
|---|
| 306 | .head = 0, |
|---|
| 307 | .cylinder = 0, |
|---|
| 308 | .firstsector = 0, /* MS-DOS, unlike the BIOS, zero-base sectors */ |
|---|
| 309 | .sectors = 1, |
|---|
| 310 | .bufferoffset = 0, |
|---|
| 311 | .bufferseg = 0 |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | void write_mbr(int drive, const void *buf) |
|---|
| 315 | { |
|---|
| 316 | uint16_t rv; |
|---|
| 317 | uint8_t err; |
|---|
| 318 | |
|---|
| 319 | dprintf("write_mbr(%d,%p)", drive, buf); |
|---|
| 320 | |
|---|
| 321 | mbr.bufferoffset = (uintptr_t) buf; |
|---|
| 322 | mbr.bufferseg = data_segment(); |
|---|
| 323 | |
|---|
| 324 | rv = 0x440d; |
|---|
| 325 | asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv) |
|---|
| 326 | :"c"(0x0841), "d"(&mbr), "b"(drive), "m"(mbr)); |
|---|
| 327 | |
|---|
| 328 | dprintf(" rv(0841) = %04x", rv); |
|---|
| 329 | if (!err) { |
|---|
| 330 | dprintf("\n"); |
|---|
| 331 | return; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | rv = 0x440d; |
|---|
| 335 | asm volatile ("int $0x21 ; setc %0" : "=bcdm" (err), "+a"(rv) |
|---|
| 336 | :"c"(0x4841), "d"(&mbr), "b"(drive), "m"(mbr)); |
|---|
| 337 | |
|---|
| 338 | dprintf(" rv(4841) = %04x\n", rv); |
|---|
| 339 | if (err) |
|---|
| 340 | die("mbr write error"); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | void read_mbr(int drive, const void *buf) |
|---|
| 344 | { |
|---|
| 345 | uint16_t rv; |
|---|
| 346 | uint8_t err; |
|---|
| 347 | |
|---|
| 348 | dprintf("read_mbr(%d,%p)", drive, buf); |
|---|
| 349 | |
|---|
| 350 | mbr.bufferoffset = (uintptr_t) buf; |
|---|
| 351 | mbr.bufferseg = data_segment(); |
|---|
| 352 | |
|---|
| 353 | rv = 0x440d; |
|---|
| 354 | asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv) |
|---|
| 355 | :"c"(0x0861), "d"(&mbr), "b"(drive), "m"(mbr)); |
|---|
| 356 | |
|---|
| 357 | dprintf(" rv(0861) = %04x", rv); |
|---|
| 358 | if (!err) { |
|---|
| 359 | dprintf("\n"); |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | rv = 0x440d; |
|---|
| 364 | asm volatile ("int $0x21 ; setc %0":"=abcdm" (err), "+a"(rv) |
|---|
| 365 | :"c"(0x4861), "d"(&mbr), "b"(drive), "m"(mbr)); |
|---|
| 366 | |
|---|
| 367 | dprintf(" rv(4841) = %04x\n", rv); |
|---|
| 368 | if (err) |
|---|
| 369 | die("mbr read error"); |
|---|
| 370 | |
|---|
| 371 | dprintf("Bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n", |
|---|
| 372 | ((const uint8_t *)buf)[0], |
|---|
| 373 | ((const uint8_t *)buf)[1], |
|---|
| 374 | ((const uint8_t *)buf)[2], |
|---|
| 375 | ((const uint8_t *)buf)[3], |
|---|
| 376 | ((const uint8_t *)buf)[4], |
|---|
| 377 | ((const uint8_t *)buf)[5], |
|---|
| 378 | ((const uint8_t *)buf)[6], |
|---|
| 379 | ((const uint8_t *)buf)[7]); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | /* This call can legitimately fail, and we don't care, so ignore error return */ |
|---|
| 383 | void set_attributes(const char *file, int attributes) |
|---|
| 384 | { |
|---|
| 385 | uint16_t rv = 0x4301; |
|---|
| 386 | |
|---|
| 387 | dprintf("set_attributes(\"%s\", 0x%02x)\n", file, attributes); |
|---|
| 388 | |
|---|
| 389 | asm volatile ("int $0x21":"+a" (rv) |
|---|
| 390 | :"c"(attributes), "d"(file)); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /* |
|---|
| 394 | * Version of the read_device function suitable for libfat |
|---|
| 395 | */ |
|---|
| 396 | int libfat_xpread(intptr_t pp, void *buf, size_t secsize, |
|---|
| 397 | libfat_sector_t sector) |
|---|
| 398 | { |
|---|
| 399 | read_device(pp, buf, 1, sector); |
|---|
| 400 | return secsize; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | static inline void get_dos_version(void) |
|---|
| 404 | { |
|---|
| 405 | uint16_t ver; |
|---|
| 406 | |
|---|
| 407 | asm("int $0x21 ; xchgb %%ah,%%al" |
|---|
| 408 | : "=a" (ver) |
|---|
| 409 | : "a" (0x3001) |
|---|
| 410 | : "ebx", "ecx"); |
|---|
| 411 | dos_version = ver; |
|---|
| 412 | |
|---|
| 413 | dprintf("DOS version %d.%d\n", (dos_version >> 8), dos_version & 0xff); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | /* The locking interface relies on static variables. A massive hack :( */ |
|---|
| 417 | static uint8_t lock_level, lock_drive; |
|---|
| 418 | |
|---|
| 419 | static inline void set_lock_device(uint8_t device) |
|---|
| 420 | { |
|---|
| 421 | lock_level = 0; |
|---|
| 422 | lock_drive = device; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | static int do_lock(uint8_t level) |
|---|
| 426 | { |
|---|
| 427 | uint16_t level_arg = lock_drive + (level << 8); |
|---|
| 428 | uint16_t rv; |
|---|
| 429 | uint8_t err; |
|---|
| 430 | #if 0 |
|---|
| 431 | /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */ |
|---|
| 432 | uint16_t lock_call = (dos_version >= 0x070a) ? 0x484A : 0x084A; |
|---|
| 433 | #else |
|---|
| 434 | uint16_t lock_call = 0x084A; /* MSDN says this is OK for all filesystems */ |
|---|
| 435 | #endif |
|---|
| 436 | |
|---|
| 437 | dprintf("Trying lock %04x... ", level_arg); |
|---|
| 438 | asm volatile ("int $0x21 ; setc %0" |
|---|
| 439 | : "=bcdm" (err), "=a" (rv) |
|---|
| 440 | : "a" (0x440d), "b" (level_arg), |
|---|
| 441 | "c" (lock_call), "d" (0x0001)); |
|---|
| 442 | dprintf("%s %04x\n", err ? "err" : "ok", rv); |
|---|
| 443 | |
|---|
| 444 | return err ? rv : 0; |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | void lock_device(int level) |
|---|
| 448 | { |
|---|
| 449 | static int hard_lock = 0; |
|---|
| 450 | int err; |
|---|
| 451 | |
|---|
| 452 | if (dos_version < 0x0700) |
|---|
| 453 | return; /* Win9x/NT only */ |
|---|
| 454 | |
|---|
| 455 | if (!hard_lock) { |
|---|
| 456 | /* Assume hierarchial "soft" locking supported */ |
|---|
| 457 | |
|---|
| 458 | while (lock_level < level) { |
|---|
| 459 | int new_level = lock_level + 1; |
|---|
| 460 | err = do_lock(new_level); |
|---|
| 461 | if (err) { |
|---|
| 462 | if (err == 0x0001) { |
|---|
| 463 | /* Try hard locking next */ |
|---|
| 464 | hard_lock = 1; |
|---|
| 465 | } |
|---|
| 466 | goto soft_fail; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | lock_level = new_level; |
|---|
| 470 | } |
|---|
| 471 | return; |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | soft_fail: |
|---|
| 475 | if (hard_lock) { |
|---|
| 476 | /* Hard locking, only level 4 supported */ |
|---|
| 477 | /* This is needed for Win9x in DOS mode */ |
|---|
| 478 | |
|---|
| 479 | err = do_lock(4); |
|---|
| 480 | if (err) { |
|---|
| 481 | if (err == 0x0001) { |
|---|
| 482 | /* Assume locking is not needed */ |
|---|
| 483 | return; |
|---|
| 484 | } |
|---|
| 485 | goto hard_fail; |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | lock_level = 4; |
|---|
| 489 | return; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | hard_fail: |
|---|
| 493 | die("could not lock device"); |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | void unlock_device(int level) |
|---|
| 497 | { |
|---|
| 498 | uint16_t rv; |
|---|
| 499 | uint8_t err; |
|---|
| 500 | uint16_t unlock_call; |
|---|
| 501 | |
|---|
| 502 | if (dos_version < 0x0700) |
|---|
| 503 | return; /* Win9x/NT only */ |
|---|
| 504 | |
|---|
| 505 | #if 0 |
|---|
| 506 | /* DOS 7.10 = Win95 OSR2 = first version with FAT32 */ |
|---|
| 507 | unlock_call = (dos_version >= 0x070a) ? 0x486A : 0x086A; |
|---|
| 508 | #else |
|---|
| 509 | unlock_call = 0x086A; /* MSDN says this is OK for all filesystems */ |
|---|
| 510 | #endif |
|---|
| 511 | |
|---|
| 512 | if (lock_level == 4 && level > 0) |
|---|
| 513 | return; /* Only drop the hard lock at the end */ |
|---|
| 514 | |
|---|
| 515 | while (lock_level > level) { |
|---|
| 516 | uint8_t new_level = (lock_level == 4) ? 0 : lock_level - 1; |
|---|
| 517 | uint16_t level_arg = (new_level << 8) + lock_drive; |
|---|
| 518 | rv = 0x440d; |
|---|
| 519 | dprintf("Trying unlock %04x... ", new_level); |
|---|
| 520 | asm volatile ("int $0x21 ; setc %0" |
|---|
| 521 | : "=bcdm" (err), "+a" (rv) |
|---|
| 522 | : "b" (level_arg), "c" (unlock_call)); |
|---|
| 523 | dprintf("%s %04x\n", err ? "err" : "ok", rv); |
|---|
| 524 | lock_level = new_level; |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | /* |
|---|
| 529 | * This function does any desired MBR manipulation; called with the device lock held. |
|---|
| 530 | */ |
|---|
| 531 | struct mbr_entry { |
|---|
| 532 | uint8_t active; /* Active flag */ |
|---|
| 533 | uint8_t bhead; /* Begin head */ |
|---|
| 534 | uint8_t bsector; /* Begin sector */ |
|---|
| 535 | uint8_t bcylinder; /* Begin cylinder */ |
|---|
| 536 | uint8_t filesystem; /* Filesystem value */ |
|---|
| 537 | uint8_t ehead; /* End head */ |
|---|
| 538 | uint8_t esector; /* End sector */ |
|---|
| 539 | uint8_t ecylinder; /* End cylinder */ |
|---|
| 540 | uint32_t startlba; /* Start sector LBA */ |
|---|
| 541 | uint32_t sectors; /* Length in sectors */ |
|---|
| 542 | } __attribute__ ((packed)); |
|---|
| 543 | |
|---|
| 544 | static void adjust_mbr(int device, int writembr, int set_active) |
|---|
| 545 | { |
|---|
| 546 | static unsigned char sectbuf[SECTOR_SIZE]; |
|---|
| 547 | int i; |
|---|
| 548 | |
|---|
| 549 | if (!writembr && !set_active) |
|---|
| 550 | return; /* Nothing to do */ |
|---|
| 551 | |
|---|
| 552 | read_mbr(device, sectbuf); |
|---|
| 553 | |
|---|
| 554 | if (writembr) { |
|---|
| 555 | memcpy(sectbuf, syslinux_mbr, syslinux_mbr_len); |
|---|
| 556 | *(uint16_t *) (sectbuf + 510) = 0xaa55; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | if (set_active) { |
|---|
| 560 | uint32_t offset = get_partition_offset(device); |
|---|
| 561 | struct mbr_entry *me = (struct mbr_entry *)(sectbuf + 446); |
|---|
| 562 | int found = 0; |
|---|
| 563 | |
|---|
| 564 | dprintf("Searching for partition offset: %08x\n", offset); |
|---|
| 565 | |
|---|
| 566 | for (i = 0; i < 4; i++) { |
|---|
| 567 | if (me->startlba == offset) { |
|---|
| 568 | me->active = 0x80; |
|---|
| 569 | found++; |
|---|
| 570 | } else { |
|---|
| 571 | me->active = 0; |
|---|
| 572 | } |
|---|
| 573 | me++; |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | if (found < 1) { |
|---|
| 577 | die("partition not found (-a is not implemented for logical partitions)"); |
|---|
| 578 | } else if (found > 1) { |
|---|
| 579 | die("multiple aliased partitions found"); |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | write_mbr(device, sectbuf); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | int main(int argc, char *argv[]) |
|---|
| 587 | { |
|---|
| 588 | static unsigned char sectbuf[SECTOR_SIZE]; |
|---|
| 589 | int dev_fd, fd; |
|---|
| 590 | static char ldlinux_name[] = "@:\\ldlinux.sys"; |
|---|
| 591 | struct libfat_filesystem *fs; |
|---|
| 592 | libfat_sector_t s, *secp; |
|---|
| 593 | libfat_sector_t *sectors; |
|---|
| 594 | int ldlinux_sectors; |
|---|
| 595 | int32_t ldlinux_cluster; |
|---|
| 596 | int nsectors; |
|---|
| 597 | const char *errmsg; |
|---|
| 598 | int i; |
|---|
| 599 | int patch_sectors; |
|---|
| 600 | unsigned char *dp; |
|---|
| 601 | |
|---|
| 602 | dprintf("argv = %p\n", argv); |
|---|
| 603 | for (i = 0; i <= argc; i++) |
|---|
| 604 | dprintf("argv[%d] = %p = \"%s\"\n", i, argv[i], argv[i]); |
|---|
| 605 | |
|---|
| 606 | get_dos_version(); |
|---|
| 607 | |
|---|
| 608 | argv[0] = program; |
|---|
| 609 | parse_options(argc, argv, MODE_SYSLINUX_DOSWIN); |
|---|
| 610 | |
|---|
| 611 | if (!opt.device) |
|---|
| 612 | usage(EX_USAGE, MODE_SYSLINUX_DOSWIN); |
|---|
| 613 | if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once |
|---|
| 614 | || (opt.update_only > 0) || opt.menu_save || opt.offset) { |
|---|
| 615 | fprintf(stderr, |
|---|
| 616 | "At least one specified option not yet implemented" |
|---|
| 617 | " for this installer.\n"); |
|---|
| 618 | exit(1); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | /* |
|---|
| 622 | * Create an ADV in memory... this should be smarter. |
|---|
| 623 | */ |
|---|
| 624 | syslinux_reset_adv(syslinux_adv); |
|---|
| 625 | |
|---|
| 626 | /* |
|---|
| 627 | * Figure out which drive we're talking to |
|---|
| 628 | */ |
|---|
| 629 | dev_fd = (opt.device[0] & ~0x20) - 0x40; |
|---|
| 630 | if (dev_fd < 1 || dev_fd > 26 || opt.device[1] != ':' || opt.device[2]) |
|---|
| 631 | usage(EX_USAGE, MODE_SYSLINUX_DOSWIN); |
|---|
| 632 | |
|---|
| 633 | set_lock_device(dev_fd); |
|---|
| 634 | |
|---|
| 635 | lock_device(2); /* Make sure we can lock the device */ |
|---|
| 636 | read_device(dev_fd, sectbuf, 1, 0); |
|---|
| 637 | unlock_device(1); |
|---|
| 638 | |
|---|
| 639 | /* |
|---|
| 640 | * Check to see that what we got was indeed an MS-DOS boot sector/superblock |
|---|
| 641 | */ |
|---|
| 642 | if ((errmsg = syslinux_check_bootsect(sectbuf, NULL))) { |
|---|
| 643 | unlock_device(0); |
|---|
| 644 | puts(errmsg); |
|---|
| 645 | putchar('\n'); |
|---|
| 646 | exit(1); |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | ldlinux_name[0] = dev_fd | 0x40; |
|---|
| 650 | |
|---|
| 651 | set_attributes(ldlinux_name, 0); |
|---|
| 652 | fd = creat(ldlinux_name, 0); /* SYSTEM HIDDEN READONLY */ |
|---|
| 653 | write_ldlinux(fd); |
|---|
| 654 | write_file(fd, syslinux_adv, 2 * ADV_SIZE); |
|---|
| 655 | close(fd); |
|---|
| 656 | set_attributes(ldlinux_name, 0x07); /* SYSTEM HIDDEN READONLY */ |
|---|
| 657 | |
|---|
| 658 | /* |
|---|
| 659 | * Now, use libfat to create a block map. This probably |
|---|
| 660 | * should be changed to use ioctl(...,FIBMAP,...) since |
|---|
| 661 | * this is supposed to be a simple, privileged version |
|---|
| 662 | * of the installer. |
|---|
| 663 | */ |
|---|
| 664 | ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE |
|---|
| 665 | + SECTOR_SIZE - 1) >> SECTOR_SHIFT; |
|---|
| 666 | sectors = calloc(ldlinux_sectors, sizeof *sectors); |
|---|
| 667 | lock_device(2); |
|---|
| 668 | fs = libfat_open(libfat_xpread, dev_fd); |
|---|
| 669 | ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL); |
|---|
| 670 | secp = sectors; |
|---|
| 671 | nsectors = 0; |
|---|
| 672 | s = libfat_clustertosector(fs, ldlinux_cluster); |
|---|
| 673 | while (s && nsectors < ldlinux_sectors) { |
|---|
| 674 | *secp++ = s; |
|---|
| 675 | nsectors++; |
|---|
| 676 | s = libfat_nextsector(fs, s); |
|---|
| 677 | } |
|---|
| 678 | libfat_close(fs); |
|---|
| 679 | |
|---|
| 680 | /* |
|---|
| 681 | * If requested, move ldlinux.sys |
|---|
| 682 | */ |
|---|
| 683 | if (opt.directory) { |
|---|
| 684 | char new_ldlinux_name[160]; |
|---|
| 685 | char *cp = new_ldlinux_name + 3; |
|---|
| 686 | const char *sd; |
|---|
| 687 | int slash = 1; |
|---|
| 688 | |
|---|
| 689 | new_ldlinux_name[0] = dev_fd | 0x40; |
|---|
| 690 | new_ldlinux_name[1] = ':'; |
|---|
| 691 | new_ldlinux_name[2] = '\\'; |
|---|
| 692 | |
|---|
| 693 | for (sd = opt.directory; *sd; sd++) { |
|---|
| 694 | char c = *sd; |
|---|
| 695 | |
|---|
| 696 | if (c == '/' || c == '\\') { |
|---|
| 697 | if (slash) |
|---|
| 698 | continue; |
|---|
| 699 | c = '\\'; |
|---|
| 700 | slash = 1; |
|---|
| 701 | } else { |
|---|
| 702 | slash = 0; |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | *cp++ = c; |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | /* Skip if subdirectory == root */ |
|---|
| 709 | if (cp > new_ldlinux_name + 3) { |
|---|
| 710 | if (!slash) |
|---|
| 711 | *cp++ = '\\'; |
|---|
| 712 | |
|---|
| 713 | memcpy(cp, "ldlinux.sys", 12); |
|---|
| 714 | |
|---|
| 715 | set_attributes(ldlinux_name, 0); |
|---|
| 716 | if (rename(ldlinux_name, new_ldlinux_name)) |
|---|
| 717 | set_attributes(ldlinux_name, 0x07); |
|---|
| 718 | else |
|---|
| 719 | set_attributes(new_ldlinux_name, 0x07); |
|---|
| 720 | } |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | /* |
|---|
| 724 | * Patch ldlinux.sys and the boot sector |
|---|
| 725 | */ |
|---|
| 726 | i = syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode, opt.directory, NULL); |
|---|
| 727 | patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT; |
|---|
| 728 | |
|---|
| 729 | /* |
|---|
| 730 | * Overwrite the now-patched ldlinux.sys |
|---|
| 731 | */ |
|---|
| 732 | /* lock_device(3); -- doesn't seem to be needed */ |
|---|
| 733 | dp = syslinux_ldlinux; |
|---|
| 734 | for (i = 0; i < patch_sectors; i++) { |
|---|
| 735 | memcpy_from_sl(sectbuf, dp, SECTOR_SIZE); |
|---|
| 736 | dp += SECTOR_SIZE; |
|---|
| 737 | write_device(dev_fd, sectbuf, 1, sectors[i]); |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | /* |
|---|
| 741 | * Muck with the MBR, if desired, while we hold the lock |
|---|
| 742 | */ |
|---|
| 743 | adjust_mbr(dev_fd, opt.install_mbr, opt.activate_partition); |
|---|
| 744 | |
|---|
| 745 | /* |
|---|
| 746 | * To finish up, write the boot sector |
|---|
| 747 | */ |
|---|
| 748 | |
|---|
| 749 | /* Read the superblock again since it might have changed while mounted */ |
|---|
| 750 | read_device(dev_fd, sectbuf, 1, 0); |
|---|
| 751 | |
|---|
| 752 | /* Copy the syslinux code into the boot sector */ |
|---|
| 753 | syslinux_make_bootsect(sectbuf, VFAT); |
|---|
| 754 | |
|---|
| 755 | /* Write new boot sector */ |
|---|
| 756 | if (opt.bootsecfile) { |
|---|
| 757 | unlock_device(0); |
|---|
| 758 | fd = creat(opt.bootsecfile, 0x20); /* ARCHIVE */ |
|---|
| 759 | write_file(fd, sectbuf, SECTOR_SIZE); |
|---|
| 760 | close(fd); |
|---|
| 761 | } else { |
|---|
| 762 | write_device(dev_fd, sectbuf, 1, 0); |
|---|
| 763 | unlock_device(0); |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | /* Done! */ |
|---|
| 767 | |
|---|
| 768 | return 0; |
|---|
| 769 | } |
|---|