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>
79 #include "syslxopt.h" /* unified options */
81 extern const char *program; /* Name of program */
84 char *mntpath = NULL; /* Path on which to mount */
89 #define boot_image syslinux_ldlinux
90 #define boot_image_len syslinux_ldlinux_len
93 int loop_fd = -1; /* Loop device */
96 void __attribute__ ((noreturn)) die(const char *msg)
98 fprintf(stderr, "%s: %s\n", program, msg);
102 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
117 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
123 if (fstat(dev_fd, &st) < 0)
128 if (!S_ISBLK(st.st_mode)) {
129 /* It's file, need to mount it loopback */
131 struct loop_info64 loopinfo;
134 for (n = 0; loop_fd < 0; n++) {
135 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
136 loop_fd = open(devfdname, O_RDWR);
137 if (loop_fd < 0 && errno == ENOENT) {
138 die("no available loopback device!");
140 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
144 die("cannot set up loopback device");
149 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
150 (loopinfo.lo_offset = opt.offset,
151 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
152 die("cannot set up loopback device");
157 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
158 (unsigned long)mypid, dev_fd);
162 return mount(devfdname, mntpath, fstype,
163 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
167 char devfdname[128], mnt_opts[128];
171 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
172 (unsigned long)mypid, dev_fd);
178 if (!S_ISBLK(st.st_mode)) {
179 snprintf(mnt_opts, sizeof mnt_opts,
180 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
181 (unsigned long long)opt.offset);
183 snprintf(mnt_opts, sizeof mnt_opts,
184 "rw,nodev,noexec,umask=077,quiet");
186 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
187 devfdname, mntpath, NULL);
188 _exit(255); /* execl failed */
191 w = waitpid(f, &status, 0);
192 return (w != f || status) ? -1 : 0;
200 void do_umount(const char *mntpath, int cookie)
203 int loop_fd = cookie;
205 if (umount2(mntpath, 0))
206 die("could not umount path");
209 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
223 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
226 w = waitpid(f, &status, 0);
227 if (w != f || status) {
234 * Make any user-specified ADV modifications
241 if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
242 fprintf(stderr, "%s: not enough space for boot-once command\n",
252 * Modify the ADV of an existing installation
254 int modify_existing_adv(const char *path)
257 syslinux_reset_adv(syslinux_adv);
258 else if (read_adv(path, "ldlinux.sys") < 0)
261 if (modify_adv() < 0)
264 if (write_adv(path, "ldlinux.sys") < 0)
270 int main(int argc, char *argv[])
272 static unsigned char sectbuf[SECTOR_SIZE];
280 uint32_t *sectors = NULL;
281 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
289 parse_options(argc, argv, MODE_SYSLINUX);
291 subdir = opt.directory;
294 usage(EX_USAGE, MODE_SYSLINUX);
297 * First make sure we can open the device at all, and that we have
298 * read/write permission.
300 dev_fd = open(opt.device, O_RDWR);
301 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
306 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
307 die("not a device or regular file");
310 if (opt.offset && S_ISBLK(st.st_mode)) {
311 die("can't combine an offset with a block device");
315 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
319 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
321 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
322 fprintf(stderr, "%s: %s\n", opt.device, errmsg);
327 * Now mount the device.
330 die("This program needs root privilege");
336 /* We're root or at least setuid.
337 Make a temp dir and pass all the gunky options to mount. */
339 if (chdir(_PATH_TMP)) {
343 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
345 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
346 (dst.st_mode & TMP_MODE) != TMP_MODE) {
347 die("possibly unsafe " _PATH_TMP " permissions");
351 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
352 (unsigned long)mypid, i);
354 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
357 rv = mkdir(mntname, 0000);
360 if (errno == EEXIST || errno == EINTR)
366 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
368 die("someone is trying to symlink race us!");
370 break; /* OK, got something... */
376 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
377 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
382 ldlinux_path = alloca(strlen(mntpath) + (subdir ? strlen(subdir) + 2 : 0));
383 sprintf(ldlinux_path, "%s%s%s",
384 mntpath, subdir ? "//" : "", subdir ? subdir : "");
386 ldlinux_name = alloca(strlen(ldlinux_path) + 14);
392 sprintf(ldlinux_name, "%s//ldlinux.sys", ldlinux_path);
394 /* update ADV only ? */
395 if (opt.update_only == -1) {
396 if (opt.reset_adv || opt.set_once) {
397 modify_existing_adv(ldlinux_path);
398 do_umount(mntpath, mnt_cookie);
403 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
408 /* Read a pre-existing ADV, if already installed */
410 syslinux_reset_adv(syslinux_adv);
411 else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
412 syslinux_reset_adv(syslinux_adv);
413 if (modify_adv() < 0)
416 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
417 uint32_t zero_attr = 0;
418 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
422 unlink(ldlinux_name);
423 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
430 /* Write it the first time */
431 if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
432 xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
433 boot_image_len) != 2 * ADV_SIZE) {
434 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
443 uint32_t attr = 0x07; /* Hidden+System+Readonly */
444 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
448 * Create a block map.
450 ldlinux_sectors += 2; /* 2 ADV sectors */
451 sectors = calloc(ldlinux_sectors, sizeof *sectors);
452 if (sectmap(fd, sectors, ldlinux_sectors)) {
460 do_umount(mntpath, mnt_cookie);
468 * Patch ldlinux.sys and the boot sector
470 i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode, opt.raid_mode, subdir);
471 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
474 * Write the now-patched first sectors of ldlinux.sys
476 for (i = 0; i < patch_sectors; i++) {
477 xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
478 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
482 * To finish up, write the boot sector
485 /* Read the superblock again since it might have changed while mounted */
486 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
488 /* Copy the syslinux code into the boot sector */
489 syslinux_make_bootsect(sectbuf);
491 /* Write new boot sector */
492 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);