summaryrefslogtreecommitdiffstats
path: root/fs/bpkfs.c
blob: 655cde09b7b7a4d1d2e50595ecee94f096886951 (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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * Copyright (c) 2013 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * Simple update file format developed for Somfy, tools and library are
 * available under LGPLv2 (https://www.gitorious.org/libbpk).
 *
 * under GPLv2 ONLY
 */

#include <common.h>
#include <driver.h>
#include <fs.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <init.h>
#include <crc.h>
#include <linux/stat.h>
#include <linux/err.h>
#include <bpkfs.h>
#include <libgen.h>

static bool bpkfs_is_crc_file(struct bpkfs_handle_data *d)
{
	return d->type & (1 << 31);
}

const char* bpkfs_type_to_str(uint32_t type)
{
	switch (type) {
	case BPKFS_TYPE_BL:
		return "bootloader";
	case BPKFS_TYPE_BLV:
		return "bootloader_version";
	case BPKFS_TYPE_DSC:
		return "description.gz";
	case BPKFS_TYPE_KER:
		return "kernel";
	case BPKFS_TYPE_RFS:
		return "rootfs";
	case BPKFS_TYPE_FMV:
		return "firmware_version";
	}

	return NULL;
}

static struct bpkfs_handle_hw *bpkfs_get_by_hw_id(
	struct bpkfs_handle *handle, uint32_t hw_id)
{
	struct bpkfs_handle_hw *h;

	list_for_each_entry(h, &handle->list, list_hw_id) {
		if (h->hw_id == hw_id)
			return h;
	}

	return NULL;
}

static struct bpkfs_handle_hw *bpkfs_hw_id_get_by_name(
	struct bpkfs_handle *handle, const char *name)
{
	struct bpkfs_handle_hw *h;

	if (!name)
		return NULL;

	list_for_each_entry(h, &handle->list, list_hw_id) {
		if (strcmp(h->name, name) == 0)
			return h;
	}

	return NULL;
}

static struct bpkfs_handle_data *bpkfs_data_get_by_name(
	struct bpkfs_handle_hw *h, const char *name)
{
	struct bpkfs_handle_data *d;

	if (!name)
		return NULL;

	list_for_each_entry(d, &h->list_data, list) {
		if (strcmp(d->name, name) == 0)
			return d;
	}

	return NULL;
}

static struct bpkfs_handle_hw *bpkfs_get_or_add_hw_id(
	struct bpkfs_handle *handle, uint32_t hw_id)
{
	struct bpkfs_handle_hw *h;

	h = bpkfs_get_by_hw_id(handle, hw_id);
	if (h)
		return h;

	h = xzalloc(sizeof(*h));

	INIT_LIST_HEAD(&h->list_data);
	h->hw_id = hw_id;
	h->name = basprintf("hw_id_%x", hw_id);
	list_add_tail(&h->list_hw_id, &handle->list);

	return h;
}

static struct bpkfs_handle_data *bpkfs_get_by_type(
	struct bpkfs_handle *handle, uint32_t hw_id, uint32_t type)
{
	struct bpkfs_handle_data *d;
	struct bpkfs_handle_hw *h;

	h = bpkfs_get_by_hw_id(handle, hw_id);
	if (!h)
		return NULL;

	list_for_each_entry(d, &h->list_data, list) {
		if (d->type == type)
			return d;
	}

	return NULL;
}

static int bpkfs_open(struct device_d *dev, FILE *f, const char *filename)
{
	struct bpkfs_handle *priv = dev->priv;
	struct bpkfs_handle_data *d;
	struct bpkfs_handle_hw *h;
	char *dir, *file;
	int ret = -EINVAL;
	char *tmp = xstrdup(filename);
	char *tmp2 = xstrdup(filename);

	dir = dirname(tmp);

	if (dir[0] == '/')
		dir++;

	h = bpkfs_hw_id_get_by_name(priv, dir);
	if (!h)
		goto out;

	file = basename(tmp2);
	d = bpkfs_data_get_by_name(h, file);
	if (!d)
		goto out;

	if (!bpkfs_is_crc_file(d)) {
		d->fd = open(priv->filename, O_RDONLY);
		if (d->fd < 0) {
			ret = d->fd;
			goto out;
		}

		lseek(d->fd, d->offset, SEEK_SET);
	}

	f->size = d->size;
	f->priv = d;
	ret = 0;

out:
	free(tmp);
	free(tmp2);
	return ret;
}

static int bpkfs_close(struct device_d *dev, FILE *file)
{
	struct bpkfs_handle_data *d = file->priv;

	close(d->fd);

	return 0;
}

static int bpkfs_read(struct device_d *dev, FILE *file, void *buf, size_t insize)
{
	struct bpkfs_handle_data *d = file->priv;

	if (bpkfs_is_crc_file(d)) {
		memcpy(buf, &d->data[d->pos], insize);
		return insize;
	} else {
		return read(d->fd, buf, insize);
	}
}

static int bpkfs_lseek(struct device_d *dev, FILE *file, loff_t pos)
{
	struct bpkfs_handle_data *d = file->priv;

	if (!bpkfs_is_crc_file(d))
		lseek(d->fd, d->offset + pos, SEEK_SET);

	d->pos = pos;

	return 0;
}

struct somfy_readdir {
	struct bpkfs_handle_hw *h;
	struct bpkfs_handle_data *d;

	DIR dir;
};

static DIR *bpkfs_opendir(struct device_d *dev, const char *pathname)
{
	struct bpkfs_handle *priv = dev->priv;
	struct somfy_readdir *sdir;
	DIR *dir;

	sdir = xzalloc(sizeof(*sdir));
	dir = &sdir->dir;
	dir->priv = sdir;

	if (pathname[0] == '/')
		pathname++;

	if (!strlen(pathname)) {
		if (list_empty(&priv->list))
			return dir;

		sdir->h = list_first_entry(&priv->list,
					struct bpkfs_handle_hw, list_hw_id);
	} else {
		sdir->h = bpkfs_hw_id_get_by_name(priv, pathname);
		if (!sdir->h || list_empty(&sdir->h->list_data))
			return dir;

		sdir->d = list_first_entry(&sdir->h->list_data,
					struct bpkfs_handle_data, list);
	}

	return dir;
}

static struct dirent *bpkfs_readdir(struct device_d *dev, DIR *dir)
{
	struct bpkfs_handle *priv = dev->priv;
	struct somfy_readdir *sdir = dir->priv;
	struct bpkfs_handle_hw *h = sdir->h;
	struct bpkfs_handle_data *d = sdir->d;

	if (!h)
		return NULL;

	if (!d) {
		if (&h->list_hw_id == &priv->list)
			return NULL;

		strcpy(dir->d.d_name, h->name);
		sdir->h = list_entry(h->list_hw_id.next, struct bpkfs_handle_hw, list_hw_id);
	} else {
		if (&d->list == &h->list_data)
			return NULL;

		strcpy(dir->d.d_name, d->name);
		sdir->d = list_entry(d->list.next, struct bpkfs_handle_data, list);
	}

	return &dir->d;
}

static int bpkfs_closedir(struct device_d *dev, DIR *dir)
{
	struct somfy_readdir *sdir = dir->priv;

	free(sdir);
	return 0;
}

static int bpkfs_stat(struct device_d *dev, const char *filename, struct stat *s)
{
	struct bpkfs_handle *priv = dev->priv;
	struct bpkfs_handle_data *d;
	struct bpkfs_handle_hw *h;
	char *dir, *file;
	int ret = -EINVAL;
	char *tmp = xstrdup(filename);
	char *tmp2 = xstrdup(filename);

	dir = dirname(tmp);

	if (filename[0] == '/')
		filename++;

	if (dir[0] == '/')
		dir++;

	if (!strlen(dir)) {
		h = bpkfs_hw_id_get_by_name(priv, filename);
		if (!h)
			goto out;

		s->st_size = strlen(filename);
		s->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
		ret = 0;
		goto out;
	}
	h = bpkfs_hw_id_get_by_name(priv, dir);
	if (!h)
		goto out;

	file = basename(tmp2);
	d = bpkfs_data_get_by_name(h, file);
	if (!d)
		goto out;

	s->st_size = d->size;
	s->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;

	ret = 0;

out:
	free(tmp);
	free(tmp2);
	return ret;
}

static void bpkfs_remove_data(struct bpkfs_handle_hw *h)
{
	struct bpkfs_handle_data *d, *tmp;

	list_for_each_entry_safe(d, tmp, &h->list_data, list) {
		free(d->name);
		free(d);
	}
}

static void bpkfs_remove(struct device_d *dev)
{
	struct bpkfs_handle *priv = dev->priv;
	struct bpkfs_handle_hw *h, *tmp;

	list_for_each_entry_safe(h, tmp, &priv->list, list_hw_id) {
		bpkfs_remove_data(h);
		free(h->name);
		free(h);
	}

	free(priv);
}

static int bpkfs_probe(struct device_d *dev)
{
	struct fs_device_d *fsdev = dev_to_fs_device(dev);
	struct bpkfs_handle *priv;
	struct bpkfs_header *header;
	struct bpkfs_data_header data_header;
	int ret = 0;
	uint32_t checksum, crc;
	uint64_t size;
	int i;
	size_t offset = 0;
	char *buf;
	int fd;

	priv = xzalloc(sizeof(struct bpkfs_handle));
	INIT_LIST_HEAD(&priv->list);
	buf = xmalloc(2048);
	dev->priv = priv;

	priv->filename = fsdev->backingstore;
	dev_dbg(dev, "mount: %s\n", fsdev->backingstore);

	fd = open(fsdev->backingstore, O_RDONLY);
	if (fd < 0) {
		ret = fd;
		goto err;
	}

	header = &priv->header;

	ret = read(fd, header, sizeof(*header));
	if (ret < 0) {
		dev_err(dev, "could not read: %s (ret = %d)\n", errno_str(), ret);
		goto err;
	}

	dev_dbg(dev, "header.magic = 0x%x\n", be32_to_cpu(header->magic));
	dev_dbg(dev, "header.version = 0x%x\n", be32_to_cpu(header->version));
	dev_dbg(dev, "header.crc = 0x%x\n", be32_to_cpu(header->crc));
	dev_dbg(dev, "header.size = %llu\n", be64_to_cpu(header->size));
	dev_dbg(dev, "header.spare = %llu\n", be64_to_cpu(header->spare));

	size = be64_to_cpu(header->size);
	offset += sizeof(*header);
	size -= sizeof(*header);

	checksum = be32_to_cpu(header->crc);
	header->crc = 0;

	crc = crc32(0, header, sizeof(*header));

	for (i = 0; size; i++) {
		struct bpkfs_handle_data *d;
		struct bpkfs_handle_hw *h;
		const char *type;

		ret = read(fd, &data_header, sizeof(data_header));
		if (ret < 0) {
			dev_err(dev, "could not read: %s\n", errno_str());
			goto err;
		} else if (ret == 0) {
			dev_err(dev, "EOF: to_read %llu\n", size);
			goto err;
		}

		d = xzalloc(sizeof(*d));

		crc = crc32(crc, &data_header, sizeof(data_header));
		offset += sizeof(data_header);
		size -= sizeof(data_header);

		d->type = be32_to_cpu(data_header.type);
		d->hw_id = be32_to_cpu(data_header.hw_id);
		d->size = be64_to_cpu(data_header.size);
		d->offset = offset;
		d->crc = be32_to_cpu(data_header.crc);
		type = bpkfs_type_to_str(d->type);

		h = bpkfs_get_or_add_hw_id(priv, d->hw_id);

		if (!type) {
			type = "unknown";
			d->name = basprintf("%s_%08x", type, d->type);
		} else {
			d->name = xstrdup(type);
		}

		dev_dbg(dev, "%d: type = 0x%x => %s\n", i, d->type, d->name);
		dev_dbg(dev, "%d: size = %llu\n", i, d->size);
		dev_dbg(dev, "%d: offset = %zu\n", i, d->offset);

		dev_dbg(dev, "%d: hw_id = 0x%x => %s\n", i, h->hw_id, h->name);

		offset += d->size;
		size -= d->size;

		if (bpkfs_get_by_type(priv, d->hw_id, d->type)) {
			dev_info(dev, "ignore data %d type %s already present, ignored\n",
				 i, type);
			free(d);
			continue;
		}

		list_add_tail(&d->list, &h->list_data);
		priv->nb_data_entries++;

		ret = lseek(fd, d->size, SEEK_CUR);
		if (ret < 0) {
			dev_err(dev, "could not seek: %s\n", errno_str());
			goto err;
		}

		type = d->name;
		d = xzalloc(sizeof(*d));
		d->type = be32_to_cpu(data_header.type);
		d->name = basprintf("%s.crc", type);
		d->type |= (1 << 31);
		d->size = 8;
		sprintf(d->data, "%08x", be32_to_cpu(data_header.crc));
		list_add_tail(&d->list, &h->list_data);
	}

	if (crc != checksum) {
		dev_err(dev, "invalid crc (0x%x != 0x%x)\n", checksum, crc);
		goto err;
	}

	close(fd);
	free(buf);

	return 0;

err:
	close(fd);
	free(buf);
	bpkfs_remove(dev);

	return ret;
}

static struct fs_driver_d bpkfs_driver = {
	.open      = bpkfs_open,
	.close     = bpkfs_close,
	.read      = bpkfs_read,
	.lseek     = bpkfs_lseek,
	.opendir   = bpkfs_opendir,
	.readdir   = bpkfs_readdir,
	.closedir  = bpkfs_closedir,
	.stat      = bpkfs_stat,
	.flags     = 0,
	.type = filetype_bpk,
	.drv = {
		.probe  = bpkfs_probe,
		.remove = bpkfs_remove,
		.name = "bpkfs",
	}
};

static int bpkfs_init(void)
{
	return register_fs_driver(&bpkfs_driver);
}
coredevice_initcall(bpkfs_init);