1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 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 * This is Linux-specific by now.
18 * This is an alternate version of the installer which doesn't require
19 * mtools, but requires root privilege.
23 * If DO_DIRECT_MOUNT is 0, call mount(8)
24 * If DO_DIRECT_MOUNT is 1, call mount(2)
27 # define DO_DIRECT_MOUNT 1
29 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
33 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
34 #define _FILE_OFFSET_BITS 64
45 #include <sys/types.h>
47 #include <sys/mount.h>
49 #include <sys/ioctl.h>
50 #include <linux/fs.h> /* FIGETBSZ, FIBMAP */
51 #include <linux/msdos_fs.h> /* FAT_IOCTL_SET_ATTRIBUTES */
52 #ifndef FAT_IOCTL_SET_ATTRIBUTES
53 # define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, uint32_t)
60 # define _PATH_MOUNT "/bin/mount"
63 # define _PATH_UMOUNT "/bin/umount"
66 # define _PATH_TMP "/tmp/"
72 # include <linux/loop.h>
75 const char *program; /* Name of program */
76 const char *device; /* Device to install to */
78 char *mntpath = NULL; /* Path on which to mount */
79 off_t filesystem_offset = 0; /* Filesystem offset */
81 int loop_fd = -1; /* Loop device */
84 void __attribute__ ((noreturn)) usage(void)
86 fprintf(stderr, "Usage: %s [-sfr][-d directory][-o offset] device\n",
91 void __attribute__ ((noreturn)) die(const char *msg)
93 fprintf(stderr, "%s: %s\n", program, msg);
97 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
110 * read/write wrapper functions
112 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
114 char *bufp = (char *)buf;
119 rv = pread(fd, bufp, count, offset);
122 } else if (rv == -1) {
123 if (errno == EINTR) {
126 die(strerror(errno));
139 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
141 const char *bufp = (const char *)buf;
146 rv = pwrite(fd, bufp, count, offset);
149 } else if (rv == -1) {
150 if (errno == EINTR) {
153 die(strerror(errno));
167 * Create a block map for ldlinux.sys
169 int make_block_map(uint32_t * sectors, int len, int dev_fd, int fd)
172 int blocksize, nblock, block;
177 if (ioctl(fd, FIGETBSZ, &blocksize) < 0)
178 die("ioctl FIGETBSZ failed");
180 blocksize >>= SECTOR_SHIFT; /* sectors/block */
185 if (ioctl(fd, FIBMAP, &block) < 0)
186 die("ioctl FIBMAP failed");
188 for (i = 0; i < blocksize; i++) {
192 *sectors++ = (block * blocksize) + i;
194 len -= (1 << SECTOR_SHIFT);
204 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
210 if (fstat(dev_fd, &st) < 0)
215 if (!S_ISBLK(st.st_mode)) {
216 /* It's file, need to mount it loopback */
218 struct loop_info64 loopinfo;
221 for (n = 0; loop_fd < 0; n++) {
222 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
223 loop_fd = open(devfdname, O_RDWR);
224 if (loop_fd < 0 && errno == ENOENT) {
225 die("no available loopback device!");
227 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
231 die("cannot set up loopback device");
236 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
237 (loopinfo.lo_offset = filesystem_offset,
238 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
239 die("cannot set up loopback device");
244 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
245 (unsigned long)mypid, dev_fd);
249 return mount(devfdname, mntpath, fstype,
250 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
254 char devfdname[128], mnt_opts[128];
258 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
259 (unsigned long)mypid, dev_fd);
265 if (!S_ISBLK(st.st_mode)) {
266 snprintf(mnt_opts, sizeof mnt_opts,
267 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
268 (unsigned long long)filesystem_offset);
270 snprintf(mnt_opts, sizeof mnt_opts,
271 "rw,nodev,noexec,umask=077,quiet");
273 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
274 devfdname, mntpath, NULL);
275 _exit(255); /* execl failed */
278 w = waitpid(f, &status, 0);
279 return (w != f || status) ? -1 : 0;
287 void do_umount(const char *mntpath, int cookie)
290 int loop_fd = cookie;
292 if (umount2(mntpath, 0))
293 die("could not umount path");
296 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
310 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
313 w = waitpid(f, &status, 0);
314 if (w != f || status) {
320 int main(int argc, char *argv[])
322 static unsigned char sectbuf[SECTOR_SIZE];
324 const unsigned char *cdp;
330 char *ldlinux_name, **argp, *opt;
331 const char *subdir = NULL;
340 int force = 0; /* -f (force) option */
341 int stupid = 0; /* -s (stupid) option */
342 int raid_mode = 0; /* -r (RAID) option */
344 (void)argc; /* Unused */
353 for (argp = argv + 1; *argp; argp++) {
362 } else if (*opt == 'r') {
364 } else if (*opt == 'f') {
365 force = 1; /* Force install */
366 } else if (*opt == 'd' && argp[1]) {
368 } else if (*opt == 'o' && argp[1]) {
370 filesystem_offset = (off_t) strtoull(*++argp, NULL, 0);
387 * First make sure we can open the device at all, and that we have
388 * read/write permission.
390 dev_fd = open(device, O_RDWR);
391 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
396 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
397 die("not a device or regular file");
400 if (filesystem_offset && S_ISBLK(st.st_mode)) {
401 die("can't combine an offset with a block device");
404 xpread(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);
408 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
410 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
411 fprintf(stderr, "%s: %s\n", device, errmsg);
416 * Now mount the device.
419 die("This program needs root privilege");
425 /* We're root or at least setuid.
426 Make a temp dir and pass all the gunky options to mount. */
428 if (chdir(_PATH_TMP)) {
432 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
434 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
435 (dst.st_mode & TMP_MODE) != TMP_MODE) {
436 die("possibly unsafe " _PATH_TMP " permissions");
440 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
441 (unsigned long)mypid, i);
443 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
446 rv = mkdir(mntname, 0000);
449 if (errno == EEXIST || errno == EINTR)
455 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
457 die("someone is trying to symlink race us!");
459 break; /* OK, got something... */
465 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
466 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
471 ldlinux_name = alloca(strlen(mntpath) + 14 +
472 (subdir ? strlen(subdir) + 2 : 0));
478 sprintf(ldlinux_name, "%s%s%s//ldlinux.sys",
479 mntpath, subdir ? "//" : "", subdir ? subdir : "");
481 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
482 uint32_t zero_attr = 0;
483 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
487 unlink(ldlinux_name);
488 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
495 cdp = syslinux_ldlinux;
496 left = syslinux_ldlinux_len;
498 nb = write(fd, cdp, left);
499 if (nb == -1 && errno == EINTR)
516 uint32_t attr = 0x07; /* Hidden+System+Readonly */
517 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
521 * Create a block map.
523 ldlinux_sectors = (syslinux_ldlinux_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
524 sectors = calloc(ldlinux_sectors, sizeof *sectors);
525 nsectors = make_block_map(sectors, syslinux_ldlinux_len, dev_fd, fd);
531 do_umount(mntpath, mnt_cookie);
539 * Patch ldlinux.sys and the boot sector
541 i = syslinux_patch(sectors, nsectors, stupid, raid_mode);
542 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
545 * Write the now-patched first sectors of ldlinux.sys
547 for (i = 0; i < patch_sectors; i++) {
548 xpwrite(dev_fd, syslinux_ldlinux + i * SECTOR_SIZE, SECTOR_SIZE,
549 filesystem_offset + ((off_t) sectors[i] << SECTOR_SHIFT));
553 * To finish up, write the boot sector
556 /* Read the superblock again since it might have changed while mounted */
557 xpread(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);
559 /* Copy the syslinux code into the boot sector */
560 syslinux_make_bootsect(sectbuf);
562 /* Write new boot sector */
563 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, filesystem_offset);