forked from kexecboot/kexecboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicescan.c
402 lines (328 loc) · 9.22 KB
/
devicescan.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* kexecboot - A kexec based bootloader
*
* Copyright (c) 2008-2011 Yuri Bushmelev <[email protected]>
* Copyright (c) 2009 Omegamoon <[email protected]>
* Copyright (c) 2008 Thomas Kunze <[email protected]>
*
* 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; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "fstype/fstype.h"
#include "util.h"
#include "devicescan.h"
#include "config.h"
/* Create charlist of known filesystems */
struct charlist *scan_filesystems()
{
char *split;
struct charlist *fl;
char line[80];
fl = create_charlist(8);
addto_charlist(fl, "lvm2");
// make a list of all known filesystems
FILE *f = fopen("/proc/filesystems", "r");
if (NULL == f) {
log_msg(lg, "+ can't open /proc/filesystems: %s", ERRMSG);
free_charlist(fl);
return NULL;
}
while (fgets(line, sizeof(line), f)) {
line[strlen(line) - 1] = '\0'; /* kill last '\n' */
split = strchr(line, '\t');
split++;
addto_charlist(fl, split); /* NOTE: strdup() may return NULL */
}
fclose(f);
return fl;
}
/* Allocate bootconf structure */
struct bootconf_t *create_bootcfg(unsigned int size)
{
struct bootconf_t *bc;
bc = malloc(sizeof(*bc));
if (NULL == bc) return NULL;
bc->list = malloc( size * sizeof(*(bc->list)) );
if (NULL == bc->list) {
free(bc);
return NULL;
}
bc->size = size;
bc->fill = 0;
bc->timeout = 0;
bc->default_item = NULL;
bc->ui = GUI;
bc->debug = 0;
return bc;
}
/* Device types */
struct dtypes_t {
enum dtype_t dtype;
int device_len;
char *device;
};
static struct dtypes_t dtypes[] = {
{ DVT_STORAGE, sizeof("/dev/hd")-1, "/dev/hd" },
{ DVT_STORAGE, sizeof("/dev/sd")-1, "/dev/sd" },
{ DVT_MMC, sizeof("/dev/mmcblk")-1, "/dev/mmcblk" },
{ DVT_MTD, sizeof("/dev/mtdblock")-1, "/dev/mtdblock" },
{ DVT_UNKNOWN, 0, NULL }
};
/* Import values from cfgdata and boot to bootconf */
int addto_bootcfg(struct bootconf_t *bc, struct device_t *dev,
struct cfgdata_t *cfgdata)
{
struct boot_item_t *bi;
struct dtypes_t *dt;
int i;
kx_cfg_section *sc;
/* Go through all found config file sections */
for (i = 0; i < cfgdata->count; i++) {
sc = cfgdata->list[i];
if (!sc) continue;
bi = malloc(sizeof(*bi));
if (NULL == bi) {
DPRINTF("Can't allocate memory for new bootconf item");
return -1;
}
bi->device = strdup(dev->device);
bi->fstype = dev->fstype;
bi->blocks = dev->blocks;
bi->dtype = DVT_UNKNOWN;
for (dt = dtypes; dt->dtype != DVT_UNKNOWN; dt++) {
if ( !strncmp(bi->device, dt->device, dt->device_len) ) {
bi->dtype = dt->dtype;
}
}
/* Section-dependent data */
bi->label = sc->label;
bi->kernelpath = sc->kernelpath;
bi->cmdline = sc->cmdline;
bi->initrd = sc->initrd;
bi->icondata = sc->icondata;
bi->priority = sc->priority;
if (sc->is_default) bc->default_item = bi;
bc->list[bc->fill] = bi;
if (cfgdata->ui != bc->ui) bc->ui = cfgdata->ui;
if (cfgdata->timeout > 0) bc->timeout = cfgdata->timeout;
if (cfgdata->debug > 0) bc->debug = cfgdata->debug;
++bc->fill;
/* Resize list when needed */
if (bc->fill >= bc->size) {
struct boot_item_t **new_list;
bc->size <<= 1; /* size *= 2; */
new_list = realloc( bc->list, bc->size * sizeof(*(bc->list)) );
if (NULL == new_list) {
DPRINTF("Can't resize boot structure");
return -1;
}
bc->list = new_list;
}
} /* for */
return 0;
}
/* Free bootconf structure */
void free_bootcfg(struct bootconf_t *bc)
{
int i;
for (i = 0; i < bc->fill; i++) {
free(bc->list[i]->device);
free(bc->list[i]->kernelpath);
dispose(bc->list[i]->cmdline);
dispose(bc->list[i]->initrd);
dispose(bc->list[i]->label);
free(bc->list[i]);
}
free(bc->list);
free(bc);
}
#ifdef DEBUG
void print_bootcfg(struct bootconf_t *bc)
{
int i;
log_msg(lg, "== Bootconf (%d, %d)", bc->size, bc->fill);
log_msg(lg, " + ui: %d", bc->ui);
log_msg(lg, " + timeout: %d", bc->timeout);
log_msg(lg, " + debug: %d", bc->debug);
for (i = 0; i < bc->fill; i++) {
log_msg(lg, " [%d] device: '%s'", i, bc->list[i]->device);
log_msg(lg, " [%d] fstype: '%s'", i, bc->list[i]->fstype);
log_msg(lg, " [%d] blocks: '%lu'", i, bc->list[i]->blocks);
log_msg(lg, " [%d] label: '%s'", i, bc->list[i]->label);
log_msg(lg, " [%d] kernelpath: '%s'", i, bc->list[i]->kernelpath);
log_msg(lg, " [%d] cmdline: '%s'", i, bc->list[i]->cmdline);
log_msg(lg, " [%d] initrd: '%s'", i, bc->list[i]->initrd);
log_msg(lg, " [%d] icondata: '%p'", i, bc->list[i]->icondata);
log_msg(lg, " [%d] priority: '%d'", i, bc->list[i]->priority);
}
}
#endif
/* Detect FS type on device and returt pointer to static structure from fstype.c */
const char *detect_fstype(char *device, struct charlist *fl)
{
int fd;
const char *fstype;
fd = open(device, O_RDONLY);
if (fd < 0) {
log_msg(lg, "+ can't open device: %s", ERRMSG);
return NULL;
}
if ( 0 != identify_fs(fd, &fstype, NULL, 0) ) {
close(fd);
log_msg(lg, "+ can't identify FS type");
return NULL;
}
close(fd);
log_msg(lg, "+ FS type '%s' detected", fstype);
/* Check that FS is known */
if (in_charlist(fl, fstype) < 0) {
/* whitelist 'ubi', we assume it is ubifs */
if (!strncmp(fstype, "ubi",3)) {
log_msg(lg, "+ found %s container: assume ubifs", fstype);
} else {
log_msg(lg, "+ FS %s is not supported by kernel", fstype);
return NULL;
}
}
return(fstype);
}
/* Check and parse config file */
int get_bootinfo(struct cfgdata_t *cfgdata)
{
struct stat sinfo;
/* Clean cfgdata structure */
init_cfgdata(cfgdata);
/* Parse config file */
if (0 == parse_cfgfile(BOOTCFG_PATH, cfgdata)) { /* Found and parsed */
log_msg(lg, "+ config file found");
/* Check kernel presence
* FIXME: we should stat every kernel or shouldn't stat at all
if (0 == stat(cfgdata->kernelpath, &sinfo)) return 0;
log_msg(lg, "+ config file points to non-existent kernel");
return -1;
*/
return 0;
} else { /* No config file found. Check kernels. */
#ifdef USE_MACHINE_KERNEL
/* Check machine kernel if set */
if (NULL != machine_kernel) {
if (0 == stat(machine_kernel, &sinfo)) {
cfgdata_add_kernel(cfgdata, machine_kernel);
log_msg(lg, "+ found machine kernel '%s'", machine_kernel);
return 0;
}
}
#endif
/* Check default kernels */
char **kp;
for (kp = default_kernels; NULL != *kp; kp++) {
if (0 == stat(*kp, &sinfo)) {
cfgdata_add_kernel(cfgdata, *kp);
log_msg(lg, "+ found default kernel '%s'", *kp);
return 0;
}
}
}
/* We have no kernels */
log_msg(lg, "+ no config file nor any kernels found");
return -1;
}
FILE *devscan_open(struct charlist **fslist)
{
FILE *f;
struct charlist *fl;
char line[80];
/* Get a list of all filesystems registered in the kernel */
fl = scan_filesystems();
if (NULL == fl) {
return NULL;
}
/* Get a list of available partitions on all devices
* See kernel/block/genhd.c for details on interface */
f = fopen("/proc/partitions", "r");
if (NULL == f) {
log_msg(lg, "Can't open /proc/partitions: %s", ERRMSG);
goto free_fl;
}
// First two lines are bogus.
fgets(line, sizeof(line), f);
fgets(line, sizeof(line), f);
*fslist = fl;
return f;
free_fl:
free_charlist(fl);
return NULL;
}
int devscan_next(FILE *fp, struct charlist *fslist, struct device_t *dev)
{
int major, minor, len;
unsigned long long blocks;
char *tmp, *p;
char *device;
char line[80];
if (NULL == fgets(line, sizeof(line), fp)) {
return 0;
}
/* Get major, minor, blocks and device name */
len = 0;
major = get_nni(line, &p);
minor = get_nni(p, &p);
blocks = get_nnll(p, &p, &len); /* len is used as temp variable */
tmp = get_word(p, &p);
if (major < 0 || minor < 0 || NULL == tmp) {
log_msg(lg, "Can't parse partition string: '%s'", line);
return -1;
}
/* FIXME: 200k is hardcoded below */
if ((0 == len) && (blocks < 200)) {
log_msg(lg, "+ device (%d, %d) is too small (%dk < 200k), skipped", major, minor, blocks);
return -1;
}
/* Format device name */
len = p - tmp;
device = malloc(len + 5 + 1); /* 5 = strlen("/dev/") */
if (NULL == device) {
DPRINTF("Can't allocate memory for device name '%s'", tmp);
return -1;
}
strcpy(device, "/dev/");
strncat(device, tmp, len);
log_msg(lg, "Found device '%s' (%d, %d) of size %lluMb",
device, major, minor, blocks>>10);
#ifdef USE_DEVICES_RECREATING
/* Remove old device node. We don't care about unlink() result. */
unlink(device);
/* Re-create device node */
log_msg(lg, "+ creating device node");
if ( -1 == mknod( device,
(S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH),
makedev(major, minor) ) )
{
log_msg(lg, "+ mknod failed: %s", ERRMSG);
}
#endif
dev->fstype = detect_fstype(device, fslist);
if (NULL == dev->fstype) {
free(device);
return -1;
}
dev->device = device;
dev->blocks = blocks;
return 1;
}