aboutsummaryrefslogtreecommitdiffstats
path: root/syslinux.c
blob: 7088fc0805faa5aea3f9bfc30801af25689b9dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 1998 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * syslinux.c - Linux installer program for SYSLINUX
 *
 * This program ought to be portable.  I hope so, at least.
 *
 * HPA note: this program needs too much privilege.  We should probably
 * access the filesystem directly like mtools does so we don't have to
 * mount the disk.  Either that or if Linux gets an fmount() system call
 * we probably could do the mounting ourselves, and make this program
 * setuid safe.
 */

#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
#include <paths.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifndef _PATH_MOUNT
#define _PATH_MOUNT "/bin/mount"
#endif

extern const unsigned char bootsect[];
extern const unsigned int  bootsect_len;

extern const unsigned char ldlinux[];
extern const unsigned int  ldlinux_len;

char *program;			/* Name of program */
char *device;			/* Device to install to */

enum bs_offsets {
  bsJump          = 0x00,
  bsOemName       = 0x03,
  bsBytesPerSec   = 0x0b,
  bsSecPerClust   = 0x0d,
  bsResSectors    = 0x0e,
  bsFATs          = 0x10,
  bsRootDirEnts   = 0x11,
  bsSectors       = 0x13,
  bsMedia         = 0x15,
  bsFATsecs       = 0x16,
  bsSecPerTrack   = 0x18,
  bsHeads         = 0x1a,
  bsHiddenSecs    = 0x1c,
  bsHugeSectors   = 0x20,
  bsDriveNumber   = 0x24,
  bsReserved1     = 0x25,
  bsBootSignature = 0x26,
  bsVolumeID      = 0x27,
  bsVolumeLabel   = 0x2b,
  bsFileSysType   = 0x36,
  bsCode          = 0x3e,
  bsSignature     = 0x1fe
};

#define bsCopyStart bsBytesPerSec
#define bsCopyLen   (bsCode-bsBytesPerSec)

/*
 * Access functions for littleendian numbers, possibly misaligned.
 */
static u_int16_t get_16(unsigned char *p)
{
  return (u_int16_t)p[0] + ((u_int16_t)p[1] << 8);
}

static u_int32_t get_32(unsigned char *p)
{
  return (u_int32_t)p[0] + ((u_int32_t)p[1] << 8) +
    ((u_int32_t)p[2] << 16) + ((u_int32_t)p[3] << 24);
}

int main(int argc, char *argv[])
{
  static unsigned char sectbuf[512], *dp;
  int dev_fd;
  struct stat st;
  int nb, left, veryold;
  unsigned int sectors, clusters;

  program = argv[0];

  if ( argc != 2 ) {
    fprintf(stderr, "Usage: %s device\n", program);
    exit(1);
  }

  device = argv[1];

  /*
   * First make sure we can open the device at all, and that we have
   * read/write permission.
   */
  dev_fd = open(device, O_RDWR);
  if ( dev_fd < 0 || fstat(dev_fd, &st) < 0 ) {
    perror(device);
    exit(1);
  }

  if ( !S_ISBLK(st.st_mode) || !S_ISREG(st.st_mode) ) {
    fprintf(stderr, "%s: not a block device or regular file\n", device);
    exit(1);
  }

  left = 512;
  dp = sectbuf;
  while ( left ) {
    nb = read(dev_fd, dp, left);
    if ( nb == -1 && errno == EINTR )
      continue;
    if ( nb < 0 ) {
      perror(device);
      exit(1);
    } else if ( nb == 0 ) {
      fprintf(stderr, "%s: no boot sector\n", device);
      exit(1);
    }
    dp += nb;
    left -= nb;
  }
  close(dev_fd);
  
  /*
   * Check to see that what we got was indeed an MS-DOS boot sector/superblock
   */

  if ( sectbuf[bsBootSignature] == 0x29 ) {
    /* It's DOS, and it has all the new nice fields */

    veryold = 0;

    sectors = get_16(sectbuf+bsSectors);
    sectors = sectors ? sectors : get_32(sectbuf+bsHugeSectors);
    clusters = sectors / sectbuf[bsSecPerClust];

    if ( !memcmp(sectbuf+bsFileSysType, "FAT12   ", 8) ) {
      if ( clusters > 4086 ) {
	fprintf(stderr, "%s: ERROR: FAT12 but claims more than 4086 clusters\n",
		device);
	exit(1);
      }
    } else {
      fprintf(stderr, "%s: filesystem type \"%8.8s\" not supported\n",
	      device, sectbuf+bsFileSysType);
      exit(1);
    }
  } else {
    veryold = 1;

    if ( sectbuf[bsSecPerClust] & (sectbuf[bsSecPerClust] - 1) ||
	 sectbuf[bsSecPerClust] == 0 ) {
      fprintf(stderr, "%s: This doesn't look like a FAT filesystem\n",
	      device);
    }

    sectors = get_16(sectbuf+bsSectors);
    sectors = sectors ? sectors : get_32(sectbuf+bsHugeSectors);
    clusters = sectors / sectbuf[bsSecPerClust];
    
    if ( clusters > 4086 ) {
      fprintf(stderr, "%s: Only FAT12 filesystems supported\n", device);
      exit(1);
    }
  }

  if ( get_16(sectbuf+bsBytesPerSec) != 512 ) {
    fprintf(stderr, "%s: Sector sizes other than 512 not supported\n",
	    device);
    exit(1);
  }
  if ( sectbuf[bsSecPerClust] > 64 ) {
    fprintf(stderr, "%s: Cluster sizes larger than 32K not supported\n",
	    device);
  }

  /*
   * Now mount the device.  If we are non-root we need to find an fstab
   * entry for this device which has the user flag set.
   */
  if ( geteuid() ) {
    FILE *fstab;
    struct mntent *mnt;

    if ( !(fstab = setmntent(MNTTAB, "r")) ) {
      fprintf(stderr, "%s: cannot open " MNTTAB "\n", program);
    }
    
    while ( (mnt = getmntent(fstab)) ) {
      if ( !strcmp(device, mnt->mnt_fsname) ) {
	if ( !strcmp(mnt->mnt_type, "msdos") ||
	     !strcmp(mnt->mnt_type, "umsdos") ||
	     !strcmp(mnt->mnt_type, "vfat") ||
	     !strcmp(mnt->mnt_type, "uvfat") ||
	     !strcmp(mnt->mnt_type, "auto") ) {
	}
      }
    }
  }

  return 0;
}