utils: Use the host toolchain to build.
[syslinux.git] / linux / syslinux.c
blob46d5624c953ab26072102a7ea99498443b25f8af
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 * This is Linux-specific by now.
19 * This is an alternate version of the installer which doesn't require
20 * mtools, but requires root privilege.
24 * If DO_DIRECT_MOUNT is 0, call mount(8)
25 * If DO_DIRECT_MOUNT is 1, call mount(2)
27 #ifdef __KLIBC__
28 # define DO_DIRECT_MOUNT 1
29 #else
30 # define DO_DIRECT_MOUNT 0 /* glibc has broken losetup ioctls */
31 #endif
33 #define _GNU_SOURCE
34 #define _XOPEN_SOURCE 500 /* For pread() pwrite() */
35 #define _FILE_OFFSET_BITS 64
36 #include <alloca.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <inttypes.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <sys/mount.h>
50 #include "linuxioctl.h"
52 #include <paths.h>
53 #ifndef _PATH_MOUNT
54 # define _PATH_MOUNT "/bin/mount"
55 #endif
56 #ifndef _PATH_UMOUNT
57 # define _PATH_UMOUNT "/bin/umount"
58 #endif
59 #ifndef _PATH_TMP
60 # define _PATH_TMP "/tmp/"
61 #endif
63 #include "syslinux.h"
65 #if DO_DIRECT_MOUNT
66 # include <linux/loop.h>
67 #endif
69 #include <getopt.h>
70 #include <sysexits.h>
71 #include "syslxrw.h"
72 #include "syslxcom.h"
73 #include "syslxfs.h"
74 #include "setadv.h"
75 #include "syslxopt.h" /* unified options */
77 extern const char *program; /* Name of program */
79 pid_t mypid;
80 char *mntpath = NULL; /* Path on which to mount */
82 #if DO_DIRECT_MOUNT
83 int loop_fd = -1; /* Loop device */
84 #endif
86 void __attribute__ ((noreturn)) die(const char *msg)
88 fprintf(stderr, "%s: %s\n", program, msg);
90 #if DO_DIRECT_MOUNT
91 if (loop_fd != -1) {
92 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
93 close(loop_fd);
94 loop_fd = -1;
96 #endif
98 if (mntpath)
99 unlink(mntpath);
101 exit(1);
105 * Mount routine
107 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
109 struct stat st;
111 (void)cookie;
113 if (fstat(dev_fd, &st) < 0)
114 return errno;
116 #if DO_DIRECT_MOUNT
118 if (!S_ISBLK(st.st_mode)) {
119 /* It's file, need to mount it loopback */
120 unsigned int n = 0;
121 struct loop_info64 loopinfo;
122 int loop_fd;
124 for (n = 0; loop_fd < 0; n++) {
125 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
126 loop_fd = open(devfdname, O_RDWR);
127 if (loop_fd < 0 && errno == ENOENT) {
128 die("no available loopback device!");
130 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
131 close(loop_fd);
132 loop_fd = -1;
133 if (errno != EBUSY)
134 die("cannot set up loopback device");
135 else
136 continue;
139 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
140 (loopinfo.lo_offset = opt.offset,
141 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
142 die("cannot set up loopback device");
145 *cookie = loop_fd;
146 } else {
147 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
148 (unsigned long)mypid, dev_fd);
149 *cookie = -1;
152 return mount(devfdname, mntpath, fstype,
153 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
155 #else
157 char devfdname[128], mnt_opts[128];
158 pid_t f, w;
159 int status;
161 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
162 (unsigned long)mypid, dev_fd);
164 f = fork();
165 if (f < 0) {
166 return -1;
167 } else if (f == 0) {
168 if (!S_ISBLK(st.st_mode)) {
169 snprintf(mnt_opts, sizeof mnt_opts,
170 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
171 (unsigned long long)opt.offset);
172 } else {
173 snprintf(mnt_opts, sizeof mnt_opts,
174 "rw,nodev,noexec,umask=077,quiet");
176 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
177 devfdname, mntpath, NULL);
178 _exit(255); /* execl failed */
181 w = waitpid(f, &status, 0);
182 return (w != f || status) ? -1 : 0;
184 #endif
188 * umount routine
190 void do_umount(const char *mntpath, int cookie)
192 #if DO_DIRECT_MOUNT
193 int loop_fd = cookie;
195 if (umount2(mntpath, 0))
196 die("could not umount path");
198 if (loop_fd != -1) {
199 ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
200 close(loop_fd);
201 loop_fd = -1;
203 #else
204 pid_t f = fork();
205 pid_t w;
206 int status;
207 (void)cookie;
209 if (f < 0) {
210 perror("fork");
211 exit(1);
212 } else if (f == 0) {
213 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
216 w = waitpid(f, &status, 0);
217 if (w != f || status) {
218 exit(1);
220 #endif
224 * Modify the ADV of an existing installation
226 int modify_existing_adv(const char *path)
228 if (opt.reset_adv)
229 syslinux_reset_adv(syslinux_adv);
230 else if (read_adv(path, "ldlinux.sys") < 0)
231 return 1;
233 if (modify_adv() < 0)
234 return 1;
236 if (write_adv(path, "ldlinux.sys") < 0)
237 return 1;
239 return 0;
242 int do_open_file(char *name)
244 int fd;
246 if ((fd = open(name, O_RDONLY)) >= 0) {
247 uint32_t zero_attr = 0;
248 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
249 close(fd);
252 unlink(name);
253 fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
254 if (fd < 0)
255 perror(name);
257 return fd;
260 int main(int argc, char *argv[])
262 static unsigned char sectbuf[SECTOR_SIZE];
263 int dev_fd, fd;
264 struct stat st;
265 int err = 0;
266 char mntname[128];
267 char *ldlinux_name;
268 char *ldlinux_path;
269 char *subdir;
270 sector_t *sectors = NULL;
271 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
272 const char *errmsg;
273 int mnt_cookie;
274 int patch_sectors;
275 int i, rv;
277 mypid = getpid();
278 umask(077);
279 parse_options(argc, argv, MODE_SYSLINUX);
281 /* Note: subdir is guaranteed to start and end in / */
282 if (opt.directory && opt.directory[0]) {
283 int len = strlen(opt.directory);
284 int rv = asprintf(&subdir, "%s%s%s",
285 opt.directory[0] == '/' ? "" : "/",
286 opt.directory,
287 opt.directory[len-1] == '/' ? "" : "/");
288 if (rv < 0 || !subdir) {
289 perror(program);
290 exit(1);
292 } else {
293 subdir = "/";
296 if (!opt.device || opt.install_mbr || opt.activate_partition)
297 usage(EX_USAGE, MODE_SYSLINUX);
300 * First make sure we can open the device at all, and that we have
301 * read/write permission.
303 dev_fd = open(opt.device, O_RDWR);
304 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
305 perror(opt.device);
306 exit(1);
309 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
310 die("not a device or regular file");
313 if (opt.offset && S_ISBLK(st.st_mode)) {
314 die("can't combine an offset with a block device");
317 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
318 fsync(dev_fd);
321 * Check to see that what we got was indeed an FAT/NTFS
322 * boot sector/superblock
324 if ((errmsg = syslinux_check_bootsect(sectbuf, &fs_type))) {
325 fprintf(stderr, "%s: %s\n", opt.device, errmsg);
326 exit(1);
330 * Now mount the device.
332 if (geteuid()) {
333 die("This program needs root privilege");
334 } else {
335 int i = 0;
336 struct stat dst;
337 int rv;
339 /* We're root or at least setuid.
340 Make a temp dir and pass all the gunky options to mount. */
342 if (chdir(_PATH_TMP)) {
343 fprintf(stderr, "%s: Cannot access the %s directory.\n",
344 program, _PATH_TMP);
345 exit(1);
347 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
349 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
350 (dst.st_mode & TMP_MODE) != TMP_MODE) {
351 die("possibly unsafe " _PATH_TMP " permissions");
354 for (i = 0;; i++) {
355 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
356 (unsigned long)mypid, i);
358 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
359 continue;
361 rv = mkdir(mntname, 0000);
363 if (rv == -1) {
364 if (errno == EEXIST || errno == EINTR)
365 continue;
366 perror(program);
367 exit(1);
370 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
371 dst.st_uid != 0) {
372 die("someone is trying to symlink race us!");
374 break; /* OK, got something... */
377 mntpath = mntname;
380 if (fs_type == VFAT) {
381 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
382 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
383 rmdir(mntpath);
384 die("failed on mounting fat volume");
386 } else if (fs_type == NTFS) {
387 if (do_mount(dev_fd, &mnt_cookie, mntpath, "ntfs-3g")) {
388 rmdir(mntpath);
389 die("failed on mounting ntfs volume");
393 ldlinux_path = alloca(strlen(mntpath) + strlen(subdir) + 1);
394 sprintf(ldlinux_path, "%s%s", mntpath, subdir);
396 ldlinux_name = alloca(strlen(ldlinux_path) + 14);
397 if (!ldlinux_name) {
398 perror(program);
399 err = 1;
400 goto umount;
402 sprintf(ldlinux_name, "%sldlinux.sys", ldlinux_path);
404 /* update ADV only ? */
405 if (opt.update_only == -1) {
406 if (opt.reset_adv || opt.set_once) {
407 modify_existing_adv(ldlinux_path);
408 do_umount(mntpath, mnt_cookie);
409 sync();
410 rmdir(mntpath);
411 exit(0);
412 } else if (opt.update_only && !syslinux_already_installed(dev_fd)) {
413 fprintf(stderr, "%s: no previous syslinux boot sector found\n",
414 argv[0]);
415 exit(1);
416 } else {
417 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
418 opt.update_only = 0;
422 /* Read a pre-existing ADV, if already installed */
423 if (opt.reset_adv)
424 syslinux_reset_adv(syslinux_adv);
425 else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
426 syslinux_reset_adv(syslinux_adv);
427 if (modify_adv() < 0)
428 exit(1);
430 fd = do_open_file(ldlinux_name);
431 if (fd < 0) {
432 err = 1;
433 goto umount;
436 /* Write it the first time */
437 if (xpwrite(fd, (const char _force *)boot_image, boot_image_len, 0)
438 != (int)boot_image_len ||
439 xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
440 boot_image_len) != 2 * ADV_SIZE) {
441 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
442 exit(1);
445 fsync(fd);
447 * Set the attributes
450 uint32_t attr = 0x07; /* Hidden+System+Readonly */
451 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
455 * Create a block map.
457 ldlinux_sectors += 2; /* 2 ADV sectors */
458 sectors = calloc(ldlinux_sectors, sizeof *sectors);
459 if (sectmap(fd, sectors, ldlinux_sectors)) {
460 perror("bmap");
461 exit(1);
463 close(fd);
464 sync();
466 sprintf(ldlinux_name, "%sldlinux.c32", ldlinux_path);
467 fd = do_open_file(ldlinux_name);
468 if (fd < 0) {
469 err = 1;
470 goto umount;
473 rv = xpwrite(fd, (const char _force *)syslinux_ldlinuxc32,
474 syslinux_ldlinuxc32_len, 0);
475 if (rv != (int)syslinux_ldlinuxc32_len) {
476 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
477 exit(1);
480 fsync(fd);
482 * Set the attributes
485 uint32_t attr = 0x07; /* Hidden+System+Readonly */
486 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
489 close(fd);
490 sync();
492 umount:
493 do_umount(mntpath, mnt_cookie);
494 sync();
495 rmdir(mntpath);
497 if (err)
498 exit(err);
501 * Patch ldlinux.sys and the boot sector
503 i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
504 opt.raid_mode, subdir, NULL);
505 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
508 * Write the now-patched first sectors of ldlinux.sys
510 for (i = 0; i < patch_sectors; i++) {
511 xpwrite(dev_fd,
512 (const char _force *)boot_image + i * SECTOR_SIZE,
513 SECTOR_SIZE,
514 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
518 * To finish up, write the boot sector
521 /* Read the superblock again since it might have changed while mounted */
522 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
524 /* Copy the syslinux code into the boot sector */
525 syslinux_make_bootsect(sectbuf, fs_type);
527 /* Write new boot sector */
528 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
530 close(dev_fd);
531 sync();
533 /* Done! */
535 return 0;