Merge branch 'pathbased' of ssh://terminus.zytor.com/pub/git/syslinux/syslinux into...
[profile/ivi/syslinux.git] / linux / syslinux.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4  *
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.
10  *
11  * ----------------------------------------------------------------------- */
12
13 /*
14  * syslinux.c - Linux installer program for SYSLINUX
15  *
16  * This is Linux-specific by now.
17  *
18  * This is an alternate version of the installer which doesn't require
19  * mtools, but requires root privilege.
20  */
21
22 /*
23  * If DO_DIRECT_MOUNT is 0, call mount(8)
24  * If DO_DIRECT_MOUNT is 1, call mount(2)
25  */
26 #ifdef __KLIBC__
27 # define DO_DIRECT_MOUNT 1
28 #else
29 # define DO_DIRECT_MOUNT 0      /* glibc has broken losetup ioctls */
30 #endif
31
32 #define _GNU_SOURCE
33 #define _XOPEN_SOURCE 500       /* For pread() pwrite() */
34 #define _FILE_OFFSET_BITS 64
35 #include <alloca.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <inttypes.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/mount.h>
48
49 #include "linuxioctl.h"
50
51 #include <paths.h>
52 #ifndef _PATH_MOUNT
53 # define _PATH_MOUNT "/bin/mount"
54 #endif
55 #ifndef _PATH_UMOUNT
56 # define _PATH_UMOUNT "/bin/umount"
57 #endif
58 #ifndef _PATH_TMP
59 # define _PATH_TMP "/tmp/"
60 #endif
61
62 #include "syslinux.h"
63
64 #if DO_DIRECT_MOUNT
65 # include <linux/loop.h>
66 #endif
67
68 #include <getopt.h>
69 #include <sysexits.h>
70 #include "syslxcom.h"
71 #include "setadv.h"
72 #include "syslxopt.h" /* unified options */
73
74 extern const char *program;     /* Name of program */
75
76 pid_t mypid;
77 char *mntpath = NULL;           /* Path on which to mount */
78
79 /*
80  * Image file
81  */
82 #define boot_image      syslinux_ldlinux
83 #define boot_image_len  syslinux_ldlinux_len
84
85 #if DO_DIRECT_MOUNT
86 int loop_fd = -1;               /* Loop device */
87 #endif
88
89 void __attribute__ ((noreturn)) die(const char *msg)
90 {
91     fprintf(stderr, "%s: %s\n", program, msg);
92
93 #if DO_DIRECT_MOUNT
94     if (loop_fd != -1) {
95         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
96         close(loop_fd);
97         loop_fd = -1;
98     }
99 #endif
100
101     if (mntpath)
102         unlink(mntpath);
103
104     exit(1);
105 }
106
107 /*
108  * Mount routine
109  */
110 int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
111 {
112     struct stat st;
113
114     (void)cookie;
115
116     if (fstat(dev_fd, &st) < 0)
117         return errno;
118
119 #if DO_DIRECT_MOUNT
120     {
121         if (!S_ISBLK(st.st_mode)) {
122             /* It's file, need to mount it loopback */
123             unsigned int n = 0;
124             struct loop_info64 loopinfo;
125             int loop_fd;
126
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!");
132                 }
133                 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
134                     close(loop_fd);
135                     loop_fd = -1;
136                     if (errno != EBUSY)
137                         die("cannot set up loopback device");
138                     else
139                         continue;
140                 }
141
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");
146             }
147
148             *cookie = loop_fd;
149         } else {
150             snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
151                      (unsigned long)mypid, dev_fd);
152             *cookie = -1;
153         }
154
155         return mount(devfdname, mntpath, fstype,
156                      MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
157     }
158 #else
159     {
160         char devfdname[128], mnt_opts[128];
161         pid_t f, w;
162         int status;
163
164         snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
165                  (unsigned long)mypid, dev_fd);
166
167         f = fork();
168         if (f < 0) {
169             return -1;
170         } else if (f == 0) {
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);
175             } else {
176                 snprintf(mnt_opts, sizeof mnt_opts,
177                          "rw,nodev,noexec,umask=077,quiet");
178             }
179             execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
180                   devfdname, mntpath, NULL);
181             _exit(255);         /* execl failed */
182         }
183
184         w = waitpid(f, &status, 0);
185         return (w != f || status) ? -1 : 0;
186     }
187 #endif
188 }
189
190 /*
191  * umount routine
192  */
193 void do_umount(const char *mntpath, int cookie)
194 {
195 #if DO_DIRECT_MOUNT
196     int loop_fd = cookie;
197
198     if (umount2(mntpath, 0))
199         die("could not umount path");
200
201     if (loop_fd != -1) {
202         ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
203         close(loop_fd);
204         loop_fd = -1;
205     }
206 #else
207     pid_t f = fork();
208     pid_t w;
209     int status;
210     (void)cookie;
211
212     if (f < 0) {
213         perror("fork");
214         exit(1);
215     } else if (f == 0) {
216         execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
217     }
218
219     w = waitpid(f, &status, 0);
220     if (w != f || status) {
221         exit(1);
222     }
223 #endif
224 }
225
226 /*
227  * Make any user-specified ADV modifications
228  */
229 int modify_adv(void)
230 {
231     int rv = 0;
232
233     if (opt.set_once) {
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",
236                     program);
237             rv = -1;
238         }
239     }
240
241     return rv;
242 }
243
244 /*
245  * Modify the ADV of an existing installation
246  */
247 int modify_existing_adv(const char *path)
248 {
249     if (opt.reset_adv)
250         syslinux_reset_adv(syslinux_adv);
251     else if (read_adv(path, "ldlinux.sys") < 0)
252         return 1;
253
254     if (modify_adv() < 0)
255         return 1;
256
257     if (write_adv(path, "ldlinux.sys") < 0)
258         return 1;
259
260     return 0;
261 }
262
263 int main(int argc, char *argv[])
264 {
265     static unsigned char sectbuf[SECTOR_SIZE];
266     int dev_fd, fd;
267     struct stat st;
268     int err = 0;
269     char mntname[128];
270     char *ldlinux_name;
271     char *ldlinux_path;
272     char *subdir;
273     sector_t *sectors = NULL;
274     int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
275     const char *errmsg;
276     int mnt_cookie;
277     int patch_sectors;
278     int i;
279
280     mypid = getpid();
281     umask(077);
282     parse_options(argc, argv, MODE_SYSLINUX);
283
284     asprintf(&subdir, "%s%s",
285              opt.directory[0] == '/' ? "" : "/", opt.directory);
286     if (!subdir) {
287         perror(program);
288         exit(1);
289     }
290
291     if (!opt.device)
292         usage(EX_USAGE, MODE_SYSLINUX);
293
294     /*
295      * First make sure we can open the device at all, and that we have
296      * read/write permission.
297      */
298     dev_fd = open(opt.device, O_RDWR);
299     if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
300         perror(opt.device);
301         exit(1);
302     }
303
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");
306     }
307
308     if (opt.offset && S_ISBLK(st.st_mode)) {
309         die("can't combine an offset with a block device");
310     }
311
312     fs_type = VFAT;
313     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
314     fsync(dev_fd);
315
316     /*
317      * Check to see that what we got was indeed an MS-DOS boot sector/superblock
318      */
319     if ((errmsg = syslinux_check_bootsect(sectbuf))) {
320         fprintf(stderr, "%s: %s\n", opt.device, errmsg);
321         exit(1);
322     }
323
324     /*
325      * Now mount the device.
326      */
327     if (geteuid()) {
328         die("This program needs root privilege");
329     } else {
330         int i = 0;
331         struct stat dst;
332         int rv;
333
334         /* We're root or at least setuid.
335            Make a temp dir and pass all the gunky options to mount. */
336
337         if (chdir(_PATH_TMP)) {
338             perror(program);
339             exit(1);
340         }
341 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
342
343         if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
344             (dst.st_mode & TMP_MODE) != TMP_MODE) {
345             die("possibly unsafe " _PATH_TMP " permissions");
346         }
347
348         for (i = 0;; i++) {
349             snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
350                      (unsigned long)mypid, i);
351
352             if (lstat(mntname, &dst) != -1 || errno != ENOENT)
353                 continue;
354
355             rv = mkdir(mntname, 0000);
356
357             if (rv == -1) {
358                 if (errno == EEXIST || errno == EINTR)
359                     continue;
360                 perror(program);
361                 exit(1);
362             }
363
364             if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
365                 dst.st_uid != 0) {
366                 die("someone is trying to symlink race us!");
367             }
368             break;              /* OK, got something... */
369         }
370
371         mntpath = mntname;
372     }
373
374     if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
375         do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
376         rmdir(mntpath);
377         die("mount failed");
378     }
379
380     ldlinux_path = alloca(strlen(mntpath) +  (subdir ? strlen(subdir) + 2 : 0));
381     sprintf(ldlinux_path, "%s%s%s",
382             mntpath, subdir ? "//" : "", subdir ? subdir : "");
383
384     ldlinux_name = alloca(strlen(ldlinux_path) + 14);
385     if (!ldlinux_name) {
386         perror(program);
387         err = 1;
388         goto umount;
389     }
390     sprintf(ldlinux_name, "%s//ldlinux.sys", ldlinux_path);
391
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);
397             sync();
398             rmdir(mntpath);
399             exit(0);
400         } else {
401             fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
402             opt.update_only = 0;
403         }
404     }
405
406     /* Read a pre-existing ADV, if already installed */
407     if (opt.reset_adv)
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)
412         exit(1);
413
414     if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
415         uint32_t zero_attr = 0;
416         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
417         close(fd);
418     }
419
420     unlink(ldlinux_name);
421     fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
422     if (fd < 0) {
423         perror(opt.device);
424         err = 1;
425         goto umount;
426     }
427
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);
433         exit(1);
434     }
435
436     fsync(fd);
437     /*
438      * Set the attributes
439      */
440     {
441         uint32_t attr = 0x07;   /* Hidden+System+Readonly */
442         ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
443     }
444
445     /*
446      * Create a block map.
447      */
448     ldlinux_sectors += 2; /* 2 ADV sectors */
449     sectors = calloc(ldlinux_sectors, sizeof *sectors);
450     if (sectmap(fd, sectors, ldlinux_sectors)) {
451         perror("bmap");
452         exit(1);
453     }
454     close(fd);
455     sync();
456
457 umount:
458     do_umount(mntpath, mnt_cookie);
459     sync();
460     rmdir(mntpath);
461
462     if (err)
463         exit(err);
464
465     /*
466      * Patch ldlinux.sys and the boot sector
467      */
468     i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
469                        opt.raid_mode, subdir);
470     patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
471
472     /*
473      * Write the now-patched first sectors of ldlinux.sys
474      */
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));
478     }
479
480     /*
481      * To finish up, write the boot sector
482      */
483
484     /* Read the superblock again since it might have changed while mounted */
485     xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
486
487     /* Copy the syslinux code into the boot sector */
488     syslinux_make_bootsect(sectbuf);
489
490     /* Write new boot sector */
491     xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
492
493     close(dev_fd);
494     sync();
495
496     /* Done! */
497
498     return 0;
499 }