Use libfat to set the MS-DOS attributes when using the unix installer.
[profile/ivi/syslinux.git] / unix / syslinux.c
1 #ident "$Id$"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 1998-2005 H. Peter Anvin - All Rights Reserved
5  *
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.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * syslinux.c - Linux installer program for SYSLINUX
16  *
17  * This program ought to be portable.  I hope so, at least.
18  *
19  * This is an alternate version of the installer which doesn't require
20  * mtools, but requires root privilege.
21  */
22
23 /*
24  * If DO_DIRECT_MOUNT is 0, call mount(8)
25  * If DO_DIRECT_MOUNT is 1, call mount(2)
26  */
27 #ifdef __KLIBC__
28 # define DO_DIRECT_MOUNT 1
29 #else
30 # define DO_DIRECT_MOUNT 0      /* glibc has broken losetup ioctls */
31 #endif
32
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>
49
50 #include "syslinux.h"
51 #include "libfat.h"
52
53 #if DO_DIRECT_MOUNT
54
55 # include <linux/loop.h>
56
57 #else
58
59 # include <paths.h>
60 # ifndef _PATH_MOUNT
61 #  define _PATH_MOUNT "/bin/mount"
62 # endif
63 # ifndef _PATH_UMOUNT
64 #  define _PATH_UMOUNT "/bin/umount"
65 # endif
66
67 #endif
68
69 const char *program;            /* Name of program */
70 const char *device;             /* Device to install to */
71 pid_t mypid;
72 char *mntpath = NULL;           /* Path on which to mount */
73 off_t filesystem_offset = 0;    /* Filesystem offset */
74 #if DO_DIRECT_MOUNT
75 int loop_fd = -1;               /* Loop device */
76 #endif
77
78 void __attribute__((noreturn)) usage(void)
79 {
80   fprintf(stderr, "Usage: %s [-sf] [-o offset] device\n", program);
81   exit(1);
82 }
83
84 void __attribute__((noreturn)) die(const char *msg)
85 {
86   fprintf(stderr, "%s: %s\n", program, msg);
87
88 #if DO_DIRECT_MOUNT
89   if ( loop_fd != -1 ) {
90     ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
91     close(loop_fd);
92     loop_fd = -1;
93   }
94 #endif
95
96   if ( mntpath )
97     unlink(mntpath);
98
99   exit(1);
100 }
101
102 /*
103  * read/write wrapper functions
104  */
105 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
106 {
107   char *p = buf;
108   ssize_t rv;
109   ssize_t done = 0;
110
111   while ( count ) {
112     rv = pread(fd, p, count, offset);
113     if ( rv == 0 ) {
114       die("short read");
115     } else if ( rv == -1 ) {
116       if ( errno == EINTR ) {
117         continue;
118       } else {
119         perror(program);
120         exit(1);
121       }
122     } else {
123       offset += rv;
124       done += rv;
125       p += rv;
126       count -= rv;
127     }
128   }
129
130   return done;
131 }
132
133 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
134 {
135   const char *p = buf;
136   ssize_t rv;
137   ssize_t done = 0;
138
139   while ( count ) {
140     rv = pwrite(fd, p, count, offset);
141     if ( rv == 0 ) {
142       die("short write");
143     } else if ( rv == -1 ) {
144       if ( errno == EINTR ) {
145         continue;
146       } else {
147         perror(program);
148         exit(1);
149       }
150     } else {
151       offset += rv;
152       done += rv;
153       p += rv;
154       count -= rv;
155     }
156   }
157
158   return done;
159 }
160
161 /*
162  * Version of the read function suitable for libfat
163  */
164 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
165 {
166   off_t offset = (off_t)sector * secsize + filesystem_offset;
167   return xpread(pp, buf, secsize, offset);
168 }
169
170 int main(int argc, char *argv[])
171 {
172   static unsigned char sectbuf[512];
173   unsigned char *dp;
174   const unsigned char *cdp;
175   int dev_fd, fd;
176   struct stat st;
177   int nb, left;
178   int err = 0;
179   pid_t f, w;
180   int status;
181   char mntname[64], devfdname[64];
182   char *ldlinux_name, **argp, *opt;
183   int force = 0;                /* -f (force) option */
184   struct libfat_filesystem *fs;
185   struct libfat_direntry dentry;
186   libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
187   int32_t ldlinux_cluster;
188   int nsectors;
189   const char *errmsg;
190
191   (void)argc;                   /* Unused */
192
193   program = argv[0];
194   mypid = getpid();
195   
196   device = NULL;
197
198   umask(077);
199
200   for ( argp = argv+1 ; *argp ; argp++ ) {
201     if ( **argp == '-' ) {
202       opt = *argp + 1;
203       if ( !*opt )
204         usage();
205
206       while ( *opt ) {
207         if ( *opt == 's' ) {
208           syslinux_make_stupid();       /* Use "safe, slow and stupid" code */
209         } else if ( *opt == 'f' ) {
210           force = 1;            /* Force install */
211         } else if ( *opt == 'o' && argp[1] ) {
212           filesystem_offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
213         } else {
214           usage();
215         }
216         opt++;
217       }
218     } else {
219       if ( device )
220         usage();
221       device = *argp;
222     }
223   }
224
225   if ( !device )
226     usage();
227
228   /*
229    * First make sure we can open the device at all, and that we have
230    * read/write permission.
231    */
232   dev_fd = open(device, O_RDWR);
233   if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
234     perror(device);
235     exit(1);
236   }
237
238   if ( !force && !S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) ) {
239     die("not a block device or regular file (use -f to override)");
240   }
241
242   if ( !force && filesystem_offset && !S_ISREG(st.st_mode) ) {
243     die("not a regular file and an offset specified (use -f to override)");
244   }
245
246   xpread(dev_fd, sectbuf, 512, filesystem_offset);
247   fsync(dev_fd);
248
249   /*
250    * Check to see that what we got was indeed an MS-DOS boot sector/superblock
251    */
252   if( (errmsg = syslinux_check_bootsect(sectbuf)) ) {
253     fprintf(stderr, "%s: %s\n", device, errmsg);
254     exit(1);
255   }
256
257   /*
258    * Now mount the device.
259    */
260   if ( geteuid() ) {
261     die("This program needs root privilege");
262   } else {
263     int i = 0;
264     struct stat dst;
265     int rv;
266
267     /* We're root or at least setuid.
268        Make a temp dir and pass all the gunky options to mount. */
269
270     if ( chdir("/tmp") ) {
271       perror(program);
272       exit(1);
273     }
274
275 #define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
276
277     if ( stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
278          (dst.st_mode & TMP_MODE) != TMP_MODE ) {
279       die("possibly unsafe /tmp permissions");
280     }
281
282     for ( i = 0 ; ; i++ ) {
283       snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
284                (unsigned long)mypid, i);
285
286       if ( lstat(mntname, &dst) != -1 || errno != ENOENT )
287         continue;
288
289       rv = mkdir(mntname, 0000);
290
291       if ( rv == -1 ) {
292         if ( errno == EEXIST || errno == EINTR )
293           continue;
294         perror(program);
295         exit(1);
296       }
297
298       if ( lstat(mntname, &dst) || dst.st_mode != (S_IFDIR|0000) ||
299            dst.st_uid != 0 ) {
300         die("someone is trying to symlink race us!");
301       }
302       break;                    /* OK, got something... */
303     }
304
305     mntpath = mntname;
306
307 #if DO_DIRECT_MOUNT
308     if ( S_ISREG(st.st_mode) ) {
309       /* It's file, need to mount it loopback */
310       unsigned int n = 0;
311       struct loop_info64 loopinfo;
312
313       for ( n = 0 ; loop_fd < 0 ; n++ ) {
314         snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
315         loop_fd = open(devfdname, O_RDWR);
316         if ( loop_fd < 0 && errno == ENOENT ) {
317           die("no available loopback device!");
318         }
319         if ( ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd) ) {
320           close(loop_fd); loop_fd = -1;
321           if ( errno != EBUSY )
322             die("cannot set up loopback device");
323           else
324             continue;
325         }
326         
327         if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
328              (loopinfo.lo_offset = filesystem_offset,
329               ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
330           die("cannot set up loopback device");
331       }
332     } else {
333       snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
334                (unsigned long)mypid, dev_fd);
335     }
336
337     if ( mount(devfdname, mntpath, "msdos",
338                MS_NOEXEC|MS_NOSUID, "umask=077,quiet") )
339       die("could not mount filesystem");
340
341 #else
342
343     snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
344              (unsigned long)mypid, dev_fd);
345
346     f = fork();
347     if ( f < 0 ) {
348       perror(program);
349       rmdir(mntpath);
350       exit(1);
351     } else if ( f == 0 ) {
352       char mnt_opts[128];
353       if ( S_ISREG(st.st_mode) ) {
354         snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
355                  (unsigned long long)filesystem_offset);
356       } else {
357         snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
358       }
359       execl(_PATH_MOUNT, _PATH_MOUNT, "-t", "msdos", "-o", mnt_opts,\
360             devfdname, mntpath, NULL);
361       _exit(255);               /* execl failed */
362     }
363
364     w = waitpid(f, &status, 0);
365     if ( w != f || status ) {
366       rmdir(mntpath);
367       exit(1);                  /* Mount failed */
368     }
369     
370 #endif
371   }
372   
373   ldlinux_name = alloca(strlen(mntpath)+13);
374   if ( !ldlinux_name ) {
375     perror(program);
376     err = 1;
377     goto umount;
378   }
379   sprintf(ldlinux_name, "%s/ldlinux.sys", mntpath);
380
381   unlink(ldlinux_name);
382   fd = open(ldlinux_name, O_WRONLY|O_CREAT|O_TRUNC, 0444);
383   if ( fd < 0 ) {
384     perror(device);
385     err = 1;
386     goto umount;
387   }
388
389   cdp = syslinux_ldlinux;
390   left = syslinux_ldlinux_len;
391   while ( left ) {
392     nb = write(fd, cdp, left);
393     if ( nb == -1 && errno == EINTR )
394       continue;
395     else if ( nb <= 0 ) {
396       perror(device);
397       err = 1;
398       goto umount;
399     }
400
401     dp += nb;
402     left -= nb;
403   }
404
405   /*
406    * I don't understand why I need this.  Does the DOS filesystems
407    * not honour the mode passed to open()?
408    */
409   fchmod(fd, 0400);
410
411   close(fd);
412
413   sync();
414
415 umount:
416 #if DO_DIRECT_MOUNT
417
418   if ( umount2(mntpath, 0) )
419     die("could not umount path");
420
421   if ( loop_fd != -1 ) {
422     ioctl(loop_fd, LOOP_CLR_FD, 0); /* Free loop device */
423     close(loop_fd);
424     loop_fd = -1;
425   }
426
427 #else
428
429   f = fork();
430   if ( f < 0 ) {
431     perror("fork");
432     exit(1);
433   } else if ( f == 0 ) {
434     execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
435   }
436
437   w = waitpid(f, &status, 0);
438   if ( w != f || status ) {
439     exit(1);
440   }
441
442 #endif
443
444   sync();
445   rmdir(mntpath);
446
447   if ( err )
448     exit(err);
449
450   /*
451    * Now, use libfat to create a block map.  This probably
452    * should be changed to use ioctl(...,FIBMAP,...) since
453    * this is supposed to be a simple, privileged version
454    * of the installer.
455    */
456   fs = libfat_open(libfat_xpread, dev_fd);
457   ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", &dentry);
458   secp = sectors;
459   nsectors = 0;
460   s = libfat_clustertosector(fs, ldlinux_cluster);
461   while ( s && nsectors < 65 ) {
462     *secp++ = s;
463     nsectors++;
464     s = libfat_nextsector(fs, s);
465   }
466   libfat_close(fs);
467
468   /*
469    * Patch ldlinux.sys and the boot sector
470    */
471   syslinux_patch(sectors, nsectors);
472
473   /*
474    * Write the now-patched first sector of ldlinux.sys
475    */
476   xpwrite(dev_fd, syslinux_ldlinux, 512, filesystem_offset + ((off_t)sectors[0] << 9));
477
478   /*
479    * Patch the root directory to set attributes to
480    * HIDDEN|SYSTEM|READONLY
481    */
482   {
483     const unsigned char attrib = 0x07;
484     xpwrite(dev_fd, &attrib, 1, ((off_t)dentry.sector << 9)+dentry.offset+11);
485   }
486
487   /*
488    * To finish up, write the boot sector
489    */
490
491   /* Read the superblock again since it might have changed while mounted */
492   xpread(dev_fd, sectbuf, 512, filesystem_offset);
493
494   /* Copy the syslinux code into the boot sector */
495   syslinux_make_bootsect(sectbuf);
496
497   /* Write new boot sector */
498   xpwrite(dev_fd, sectbuf, 512, filesystem_offset);
499
500   close(dev_fd);
501   sync();
502
503   /* Done! */
504
505   return 0;
506 }
507