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 "linuxioctl.h"
53 # define _PATH_MOUNT "/bin/mount"
56 # define _PATH_UMOUNT "/bin/umount"
59 # define _PATH_TMP "/tmp/"
65 # include <linux/loop.h>
72 #include "syslxopt.h" /* unified options */
74 extern const char *program; /* Name of program */
77 char *mntpath = NULL; /* Path on which to mount */
82 #define boot_image syslinux_ldlinux
83 #define boot_image_len syslinux_ldlinux_len
86 int loop_fd = -1; /* Loop device */
89 void __attribute__ ((noreturn)) die(const char *msg)
91 fprintf(stderr, "%s: %s\n", program, msg);
95 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
110 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
116 if (fstat(dev_fd, &st) < 0)
121 if (!S_ISBLK(st.st_mode)) {
122 /* It's file, need to mount it loopback */
124 struct loop_info64 loopinfo;
127 for (n = 0; loop_fd < 0; n++) {
128 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
129 loop_fd = open(devfdname, O_RDWR);
130 if (loop_fd < 0 && errno == ENOENT) {
131 die("no available loopback device!");
133 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
137 die("cannot set up loopback device");
142 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
143 (loopinfo.lo_offset = opt.offset,
144 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
145 die("cannot set up loopback device");
150 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
151 (unsigned long)mypid, dev_fd);
155 return mount(devfdname, mntpath, fstype,
156 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
160 char devfdname[128], mnt_opts[128];
164 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
165 (unsigned long)mypid, dev_fd);
171 if (!S_ISBLK(st.st_mode)) {
172 snprintf(mnt_opts, sizeof mnt_opts,
173 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
174 (unsigned long long)opt.offset);
176 snprintf(mnt_opts, sizeof mnt_opts,
177 "rw,nodev,noexec,umask=077,quiet");
179 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
180 devfdname, mntpath, NULL);
181 _exit(255); /* execl failed */
184 w = waitpid(f, &status, 0);
185 return (w != f || status) ? -1 : 0;
193 void do_umount(const char *mntpath, int cookie)
196 int loop_fd = cookie;
198 if (umount2(mntpath, 0))
199 die("could not umount path");
202 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
216 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
219 w = waitpid(f, &status, 0);
220 if (w != f || status) {
227 * Make any user-specified ADV modifications
234 if (syslinux_setadv(ADV_BOOTONCE, strlen(opt.set_once), opt.set_once)) {
235 fprintf(stderr, "%s: not enough space for boot-once command\n",
245 * Modify the ADV of an existing installation
247 int modify_existing_adv(const char *path)
250 syslinux_reset_adv(syslinux_adv);
251 else if (read_adv(path, "ldlinux.sys") < 0)
254 if (modify_adv() < 0)
257 if (write_adv(path, "ldlinux.sys") < 0)
263 int main(int argc, char *argv[])
265 static unsigned char sectbuf[SECTOR_SIZE];
273 sector_t *sectors = NULL;
274 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
282 parse_options(argc, argv, MODE_SYSLINUX);
284 asprintf(&subdir, "%s%s",
285 opt.directory[0] == '/' ? "" : "/", opt.directory);
292 usage(EX_USAGE, MODE_SYSLINUX);
295 * First make sure we can open the device at all, and that we have
296 * read/write permission.
298 dev_fd = open(opt.device, O_RDWR);
299 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
304 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
305 die("not a device or regular file");
308 if (opt.offset && S_ISBLK(st.st_mode)) {
309 die("can't combine an offset with a block device");
313 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
317 * Check to see that what we got was indeed an MS-DOS boot sector/superblock
319 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
320 fprintf(stderr, "%s: %s\n", opt.device, errmsg);
325 * Now mount the device.
328 die("This program needs root privilege");
334 /* We're root or at least setuid.
335 Make a temp dir and pass all the gunky options to mount. */
337 if (chdir(_PATH_TMP)) {
341 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
343 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
344 (dst.st_mode & TMP_MODE) != TMP_MODE) {
345 die("possibly unsafe " _PATH_TMP " permissions");
349 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
350 (unsigned long)mypid, i);
352 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
355 rv = mkdir(mntname, 0000);
358 if (errno == EEXIST || errno == EINTR)
364 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
366 die("someone is trying to symlink race us!");
368 break; /* OK, got something... */
374 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
375 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
380 ldlinux_path = alloca(strlen(mntpath) + (subdir ? strlen(subdir) + 2 : 0));
381 sprintf(ldlinux_path, "%s%s%s",
382 mntpath, subdir ? "//" : "", subdir ? subdir : "");
384 ldlinux_name = alloca(strlen(ldlinux_path) + 14);
390 sprintf(ldlinux_name, "%s//ldlinux.sys", ldlinux_path);
392 /* update ADV only ? */
393 if (opt.update_only == -1) {
394 if (opt.reset_adv || opt.set_once) {
395 modify_existing_adv(ldlinux_path);
396 do_umount(mntpath, mnt_cookie);
401 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
406 /* Read a pre-existing ADV, if already installed */
408 syslinux_reset_adv(syslinux_adv);
409 else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
410 syslinux_reset_adv(syslinux_adv);
411 if (modify_adv() < 0)
414 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
415 uint32_t zero_attr = 0;
416 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
420 unlink(ldlinux_name);
421 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
428 /* Write it the first time */
429 if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
430 xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
431 boot_image_len) != 2 * ADV_SIZE) {
432 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
441 uint32_t attr = 0x07; /* Hidden+System+Readonly */
442 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
446 * Create a block map.
448 ldlinux_sectors += 2; /* 2 ADV sectors */
449 sectors = calloc(ldlinux_sectors, sizeof *sectors);
450 if (sectmap(fd, sectors, ldlinux_sectors)) {
458 do_umount(mntpath, mnt_cookie);
466 * Patch ldlinux.sys and the boot sector
468 i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
469 opt.raid_mode, subdir);
470 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
473 * Write the now-patched first sectors of ldlinux.sys
475 for (i = 0; i < patch_sectors; i++) {
476 xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
477 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
481 * To finish up, write the boot sector
484 /* Read the superblock again since it might have changed while mounted */
485 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
487 /* Copy the syslinux code into the boot sector */
488 syslinux_make_bootsect(sectbuf);
490 /* Write new boot sector */
491 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);