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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* ----------------------------------------------------------------------- *
*
* Copyright 2010 Intel Corp. - 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., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* syslxcom.c
*
* common functions for extlinux & syslinux installer
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include "linuxioctl.h"
#include "syslxcom.h"
#include "syslxfs.h"
const char *program;
int fs_type;
#ifdef DEBUG
# define dprintf printf
#else
# define dprintf(...) ((void)0)
#endif
#define SECTOR_SHIFT 9
static void die(const char *msg)
{
fputs(msg, stderr);
exit(1);
}
/*
* read/write wrapper functions
*/
ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
{
char *bufp = (char *)buf;
ssize_t rv;
ssize_t done = 0;
while (count) {
rv = pread(fd, bufp, count, offset);
if (rv == 0) {
die("short read");
} else if (rv == -1) {
if (errno == EINTR) {
continue;
} else {
die(strerror(errno));
}
} else {
bufp += rv;
offset += rv;
done += rv;
count -= rv;
}
}
return done;
}
ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
{
const char *bufp = (const char *)buf;
ssize_t rv;
ssize_t done = 0;
while (count) {
rv = pwrite(fd, bufp, count, offset);
if (rv == 0) {
die("short write");
} else if (rv == -1) {
if (errno == EINTR) {
continue;
} else {
die(strerror(errno));
}
} else {
bufp += rv;
offset += rv;
done += rv;
count -= rv;
}
}
return done;
}
/*
* Set and clear file attributes
*/
void clear_attributes(int fd)
{
struct stat st;
if (!fstat(fd, &st)) {
switch (fs_type) {
case EXT2:
{
int flags;
if (!ioctl(fd, FS_IOC_GETFLAGS, &flags)) {
flags &= ~FS_IMMUTABLE_FL;
ioctl(fd, FS_IOC_SETFLAGS, &flags);
}
break;
}
case VFAT:
{
uint32_t attr = 0x00; /* Clear all attributes */
ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
break;
}
case NTFS:
break;
default:
break;
}
fchmod(fd, st.st_mode | S_IWUSR);
}
}
void set_attributes(int fd)
{
struct stat st;
if (!fstat(fd, &st)) {
fchmod(fd, st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH));
switch (fs_type) {
case EXT2:
{
int flags;
if (st.st_uid == 0 && !ioctl(fd, FS_IOC_GETFLAGS, &flags)) {
flags |= FS_IMMUTABLE_FL;
ioctl(fd, FS_IOC_SETFLAGS, &flags);
}
break;
}
case VFAT:
{
uint32_t attr = 0x07; /* Hidden+System+Readonly */
ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
break;
}
case NTFS:
break;
default:
break;
}
}
}
/* New FIEMAP based mapping */
static int sectmap_fie(int fd, sector_t *sectors, int nsectors)
{
struct fiemap *fm;
struct fiemap_extent *fe;
unsigned int i, nsec;
sector_t sec, *secp, *esec;
struct stat st;
uint64_t maplen;
if (fstat(fd, &st))
return -1;
fm = alloca(sizeof(struct fiemap)
+ nsectors * sizeof(struct fiemap_extent));
memset(fm, 0, sizeof *fm);
maplen = (uint64_t)nsectors << SECTOR_SHIFT;
if (maplen > (uint64_t)st.st_size)
maplen = st.st_size;
fm->fm_start = 0;
fm->fm_length = maplen;
fm->fm_flags = FIEMAP_FLAG_SYNC;
fm->fm_extent_count = nsectors;
if (ioctl(fd, FS_IOC_FIEMAP, fm))
return -1;
memset(sectors, 0, nsectors * sizeof *sectors);
esec = sectors + nsectors;
fe = fm->fm_extents;
if (fm->fm_mapped_extents < 1 ||
!(fe[fm->fm_mapped_extents-1].fe_flags & FIEMAP_EXTENT_LAST))
return -1;
for (i = 0; i < fm->fm_mapped_extents; i++) {
if (fe->fe_flags & FIEMAP_EXTENT_LAST) {
/* If this is the *final* extent, pad the length */
fe->fe_length = (fe->fe_length + SECTOR_SIZE - 1)
& ~(SECTOR_SIZE - 1);
}
if ((fe->fe_logical | fe->fe_physical| fe->fe_length) &
(SECTOR_SIZE - 1))
return -1;
if (fe->fe_flags & (FIEMAP_EXTENT_UNKNOWN|
FIEMAP_EXTENT_DELALLOC|
FIEMAP_EXTENT_ENCODED|
FIEMAP_EXTENT_DATA_ENCRYPTED|
FIEMAP_EXTENT_UNWRITTEN))
return -1;
secp = sectors + (fe->fe_logical >> SECTOR_SHIFT);
sec = fe->fe_physical >> SECTOR_SHIFT;
nsec = fe->fe_length >> SECTOR_SHIFT;
while (nsec--) {
if (secp >= esec)
break;
*secp++ = sec++;
}
fe++;
}
return 0;
}
/* Legacy FIBMAP based mapping */
static int sectmap_fib(int fd, sector_t *sectors, int nsectors)
{
unsigned int blk, nblk;
unsigned int i;
unsigned int blksize;
sector_t sec;
/* Get block size */
if (ioctl(fd, FIGETBSZ, &blksize))
return -1;
/* Number of sectors per block */
blksize >>= SECTOR_SHIFT;
nblk = 0;
while (nsectors) {
blk = nblk++;
if (ioctl(fd, FIBMAP, &blk))
return -1;
sec = (sector_t)blk * blksize;
for (i = 0; i < blksize; i++) {
*sectors++ = sec++;
if (! --nsectors)
break;
}
}
return 0;
}
/*
* Produce file map
*/
int sectmap(int fd, sector_t *sectors, int nsectors)
{
if (!sectmap_fie(fd, sectors, nsectors))
return 0;
return sectmap_fib(fd, sectors, nsectors);
}
/*
* SYSLINUX installs the string 'SYSLINUX' at offset 3 in the boot
* sector; this is consistent with FAT filesystems. Earlier versions
* would install the string "EXTLINUX" instead, handle both.
*/
int syslinux_already_installed(int dev_fd)
{
char buffer[8];
xpread(dev_fd, buffer, 8, 3);
return !memcmp(buffer, "SYSLINUX", 8) || !memcmp(buffer, "EXTLINUX", 8);
}
|