File: mtd-interface.c

package info (click to toggle)
swupdate 2025.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,004 kB
  • sloc: ansic: 66,621; python: 6,291; makefile: 791; sh: 538; javascript: 229
file content (566 lines) | stat: -rw-r--r-- 12,511 bytes parent folder | download | duplicates (3)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 * (C) Copyright 2014
 * Stefano Babic, stefano.babic@swupdate.org.
 *
 * SPDX-License-Identifier:     GPL-2.0-only
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <mtd/mtd-user.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "bsdqueue.h"
#include "util.h"
#include "flash.h"

static char mtd_ubi_blacklist[100] = { 0 };

/*
 * Note: the functions here are derived directly
 * with minor changes from mtd-utils.
 */
#define EMPTY_BYTE	0xFF

int flash_erase_sector(int mtdnum, off_t start, size_t size)
{
	int fd;
	char mtd_device[80];
	struct mtd_dev_info *mtd;
	int noskipbad = 0;
	int ret = 0;
	unsigned int eb, eb_start, i, eb_end, end;
	uint8_t *buf;
	struct flash_description *flash = get_flash_info();

	if  (!mtd_dev_present(flash->libmtd, mtdnum)) {
			ERROR("MTD %d does not exist", mtdnum);
			return -ENODEV;
	}
	mtd = &flash->mtd_info[mtdnum].mtd;
	snprintf(mtd_device, sizeof(mtd_device), "/dev/mtd%d", mtdnum);

	eb_start = start;
	size = size ? size : mtd->size;
	if (!mtd->eb_size)
		return -EINVAL;

	end = start + size;
	eb_start /= mtd->eb_size;
	eb_end = end / mtd->eb_size;
	if (end % mtd->eb_size)
		eb_end++;

	if ((fd = open(mtd_device, O_RDWR)) < 0) {
		ERROR( "%s: %s: %s", __func__, mtd_device, strerror(errno));
		return -ENODEV;
	}

	/*
	 * prepare to erase all of the MTD partition,
	 */
	buf = (uint8_t *)malloc(mtd->eb_size);
	if (!buf) {
		ERROR("No memory for temporary buffer of %d bytes",
			mtd->eb_size);
		close(fd);
		return -ENOMEM;
	}

	for (eb = eb_start; eb < eb_end; eb++) {

		/* Always skip bad sectors */
		if (!noskipbad) {
			int isbad = mtd_is_bad(mtd, fd, eb);
			if (isbad > 0) {
				/* Will need to erase one more block, instead of the bad one */
				if (eb_end * mtd->eb_size < mtd->size)
					eb_end++;
				continue;
			} else if (isbad < 0) {
				if (errno == EOPNOTSUPP) {
					noskipbad = 1;
				} else {
					ERROR("%s: MTD get bad block failed", mtd_device);
					ret  = -EFAULT;
					goto erase_out;
				}
			}
		}

		/* Unlock memory if required */
		if (mtd_is_locked(mtd, fd, eb) > 0) {
			if (mtd_unlock(mtd, fd, eb) != 0) {
				if (errno != EOPNOTSUPP) {
					TRACE("%s: MTD unlock failure", mtd_device);
					continue;
				}
			}
		}

		/*
		 * In case of NOR flash, check if the flash
		 * is already empty. This can save
		 * an amount of time because erasing
		 * a NOR flash is very time expensive.
		 * NAND flash is always erased.
		 */
		if (!isNand(flash, mtdnum)) {
			if (mtd_read(mtd, fd, eb, 0, buf, mtd->eb_size) != 0) {
				ERROR("%s: MTD Read failure", mtd_device);
				ret  = -EIO;
				goto erase_out;
			}

			/* check if already empty */
			for (i = 0; i < mtd->eb_size; i++) {
				if (buf[i] != EMPTY_BYTE)
					break;
			}

			/* skip erase if empty */
			if (i == mtd->eb_size)
				continue;

		}

		/* The sector contains data and it must be erased */
		if (mtd_erase(flash->libmtd, mtd, fd, eb) != 0) {
			ERROR("%s: MTD Erase failure", mtd_device);
			ret  = -EIO;
			goto erase_out;
		}
	}

erase_out:
	free(buf);

	close(fd);

	return ret;
}

int flash_erase(int mtdnum)
{
	return flash_erase_sector(mtdnum, 0, 0);
}

void mtd_init(void)
{
	struct flash_description *flash = get_flash_info();
	flash->libmtd = libmtd_open();
	if (flash->libmtd == NULL) {
		if (errno == 0)
			WARN("MTD is not present in the system");
		WARN("cannot open libmtd");
	}
}

void mtd_set_ubiblacklist(char *mtdlist)
{
	strlcpy(mtd_ubi_blacklist, mtdlist, sizeof(mtd_ubi_blacklist));
}

int get_mtd_from_device(char *s) {
	int ret;
	int mtdnum;
	char *real_s;

	if (!s)
		return -1;

	real_s = realpath(s, NULL);
	if (real_s == NULL) {
		char tmp_s[PATH_MAX] = {0};

		if (! strncmp(s, "/dev/", 5))
			return -1;

		snprintf(tmp_s, sizeof(tmp_s), "/dev/%s", s);
		real_s = realpath(tmp_s, NULL);
		if (real_s == NULL)
			return -1;
	}

	TRACE("mtd name [%s] resolved to [%s]", s, real_s);
	ret = sscanf(real_s, "mtd%d", &mtdnum);
	if (ret <= 0)
		ret = sscanf(real_s, "/dev/mtd%d", &mtdnum);

	free (real_s);

	if (ret <= 0)
		return -1;

	return mtdnum;
}

int get_mtd_from_name(const char *s)
{
	struct flash_description *flash = get_flash_info();
	struct mtd_dev_info *info;
	int i;

	for (i = flash->mtd.lowest_mtd_num;
	     i <= flash->mtd.highest_mtd_num; i++) {
		info = &flash->mtd_info[i].mtd;
		if (!strcmp(info->name, s))
			return i;
	}

	return -1;
}

long long get_mtd_size(int mtdnum)
{
	struct flash_description *flash = get_flash_info();
	struct mtd_dev_info dev_info;

	int err = mtd_get_dev_info1(flash->libmtd, mtdnum, &dev_info);
	if (err != 0) {
		ERROR("Could not get MTD %d info: %d, %d", mtdnum, err, errno);
		return -ENODEV;
	}

	return dev_info.size;
}

void ubi_init(void)
{
	struct flash_description *nand = get_flash_info();
	int err;
	libubi_t libubi;

	libubi = libubi_open();
	if (!libubi) {
		return;
	}

	nand->libubi = libubi;

	err = ubi_get_info(libubi, &nand->ubi_info);
	if (err) {
		ERROR("cannot get UBI information");
		return;
	}

	if (nand->ubi_info.ctrl_major == -1) {
		ERROR("MTD attach/detach feature is not supported by your kernel");
	}
}

static void ubi_insert_list(int index, struct flash_description *flash, bool black)
{
	struct mtd_info *mtd = &flash->mtd;

	if (index >= mtd->lowest_mtd_num && index <= mtd->highest_mtd_num) {
		if (black) {
			flash->mtd_info[index].skipubi = 1;
			flash->mtd_info[index].has_ubi = 0;
		} else {
			flash->mtd_info[index].skipubi = 0;
			flash->mtd_info[index].has_ubi = 1;
		}
	}
}

#if defined(CONFIG_UBIVOL)
static void scan_ubi_volumes(struct mtd_ubi_info *info)
{
	struct flash_description *flash = get_flash_info();
	libubi_t libubi = flash->libubi;
	struct ubi_part *ubi_part;
	int i, err;

	for (i = info->dev_info.lowest_vol_id;
	     i <= info->dev_info.highest_vol_id; i++) {
		ubi_part = (struct ubi_part *)calloc(1, sizeof(struct ubi_part));
		if (!ubi_part) {
			ERROR("No memory: malloc failed");
			return;
		}

		err = ubi_get_vol_info1(libubi, info->dev_info.dev_num,
					i, &ubi_part->vol_info);
		if (err == -1) {
			free(ubi_part);
			if (errno == ENOENT || errno == ENODEV)
				continue;

			ERROR("libubi failed to probe volume %d on ubi%d",
					  i, info->dev_info.dev_num);
			return;
		}

		LIST_INSERT_HEAD(&info->ubi_partitions, ubi_part, next);
		TRACE("mtd%d:\tVolume found : \t%s",
			info->dev_info.mtd_num,
			ubi_part->vol_info.name);
	}

	info->scanned = 1;
}

static void scan_for_ubi_devices(void)
{
	struct flash_description *flash = get_flash_info();
	libubi_t libubi = flash->libubi;
	struct ubi_info ubi_info;
	struct ubi_dev_info dev_info;
	struct mtd_ubi_info *mtd_info;
	int err, i, mtd;

	if (!libubi)
		return;
	/*
	 * if not yet an attached device, return and try later
	 * to attach them
	 */
	err = ubi_get_info(libubi, &ubi_info);
	if (err)
		return;

	for (i = ubi_info.lowest_dev_num;
	     i <= ubi_info.highest_dev_num; i++) {
		err = ubi_get_dev_info1(libubi, i, &dev_info);
		if (err == -1) {
			continue;
		}
		mtd = dev_info.mtd_num;
		mtd_info = &flash->mtd_info[mtd];
		if (mtd < 0 || flash->mtd_info[mtd].skipubi)
			continue;
		memcpy(&mtd_info->dev_info, &dev_info, sizeof(struct ubi_dev_info));

		scan_ubi_volumes(mtd_info);
	}
}

#if defined(CONFIG_UBIATTACH)
static void scan_ubi_partitions(int mtd)
{
	struct flash_description *flash = get_flash_info();
	libubi_t libubi = flash->libubi;
	int err, tryattach = 0;
	struct mtd_ubi_info *mtd_info;

	if (mtd < 0) {
		ERROR("wrong MTD device /dev/mtd%d", mtd);
		return;
	}

	mtd_info = &flash->mtd_info[mtd];

	/*
	 * The program is called directly after a boot,
	 * and a detach is not required. However,
	 * detaching at the beginning allows consecutive
	 * start of the program itself
	 */
	mtd_info->req.dev_num = UBI_DEV_NUM_AUTO;
	mtd_info->req.mtd_num = mtd;
#if defined(CONFIG_UBIVIDOFFSET)
	mtd_info->req.vid_hdr_offset = CONFIG_UBIVIDOFFSET;
#else
	mtd_info->req.vid_hdr_offset = 0;
#endif
	mtd_info->req.mtd_dev_node = NULL;

	/*
	 * Check if the MTD was alrady attached
	 * and tries to get information, if not found
	 * try to attach.
	 */
	do {
		err = ubi_attach(libubi, DEFAULT_CTRL_DEV, &mtd_info->req);
		if (err) {
			/* Handle race condition where MTD was already being attached. */
			if (errno == EEXIST && !mtd_num2ubi_dev(libubi, mtd, &mtd_info->req.dev_num))
				break;
			if (mtd_info->has_ubi && !tryattach) {
				TRACE("cannot attach mtd%d ..try erasing", mtd);
				if (flash_erase(mtd)) {
					ERROR("mtd%d cannot be erased", mtd);
					return;
				}
			} else {
				ERROR("cannot attach mtd%d - maybe not a NAND or raw device", mtd);
				return;
			}
			tryattach++;
		}
	} while (err != 0 && tryattach < 2);

	err = ubi_get_dev_info1(libubi, mtd_info->req.dev_num, &mtd_info->dev_info);
	if (err) {
		ERROR("cannot get information about UBI device %d", mtd_info->req.dev_num);
		return;
	}

	scan_ubi_volumes(mtd_info);
}
#endif
#endif

int scan_mtd_devices (void)
{
	int err;
	struct flash_description *flash = get_flash_info();
	struct mtd_info *mtd_info = &flash->mtd;
	struct mtd_ubi_info *mtd_ubi_info;
	libmtd_t libmtd = flash->libmtd;
	char list[100];
	char *token;
	char *saveptr;
	int i, index;
	bool black;

	if (!libmtd) {
		WARN("MTD is not present on the target");
		return -1;
	}
	err = mtd_get_info(libmtd, mtd_info);
	if (err) {
		if (errno == ENODEV)
			ERROR("MTD is not present on the board");
		return 0;
	}

	/* Allocate memory to store MTD infos */
	flash->mtd_info = (struct mtd_ubi_info *)calloc(
				mtd_info->highest_mtd_num + 1,
				sizeof(struct mtd_ubi_info));
	if (!flash->mtd_info) {
		ERROR("No enough memory for MTD structures");
		return -ENOMEM;
	}

	for (i = 0; i < 2; i++) {
		memset(list, 0, sizeof(list));
		switch (i) {
		case 0:
			black = true;
#if defined(CONFIG_UBIBLACKLIST)
			strlcpy(list, CONFIG_UBIBLACKLIST,
				sizeof(list));
#endif
			/* Blacklist passed on the command line has priority */
			if (strlen(mtd_ubi_blacklist))
				strlcpy(list, mtd_ubi_blacklist, sizeof(list));
			break;
		case 1:
			black = false;
#if defined(CONFIG_UBIWHITELIST)
			strlcpy(list, CONFIG_UBIWHITELIST,
				sizeof(list));
#endif
			break;
		}

		token = strtok_r(list, " ", &saveptr);
		if (token) {
			errno = 0;
			index = strtoul(token, NULL, 10);
			if (errno == 0) {
				ubi_insert_list(index, flash, black);

				while ((token = strtok_r(NULL, " ", &saveptr))) {
					errno = 0;
					index = strtoul(token, NULL, 10);
					if (errno != 0)
						break;
					ubi_insert_list(index, flash, black);
				}
			}
		}
	}

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		/* initialize data */
		mtd_ubi_info = &flash->mtd_info[i];
		LIST_INIT(&mtd_ubi_info->ubi_partitions);
		if (!mtd_dev_present(libmtd, i))
			continue;
		err = mtd_get_dev_info1(libmtd, i, &flash->mtd_info[i].mtd);
		if (err) {
			TRACE("No information from MTD%d", i);
			continue;
		}
	}

#if defined(CONFIG_UBIVOL)
	/*
	 * Now search for MTD that are already attached
	 */
	scan_for_ubi_devices();

#if defined(CONFIG_UBIATTACH)
	/*
	 * Search for volumes in MTD that are not attached, default case
	 */

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		if (flash->libubi && !flash->mtd_info[i].skipubi &&
				!flash->mtd_info[i].scanned &&
				flash->mtd_info[i].mtd.type != MTD_UBIVOLUME)
			scan_ubi_partitions(i);
	}
#endif
#endif

	return mtd_info->mtd_dev_cnt;
}

void ubi_mount(struct ubi_vol_info *vol, const char *mntpoint)
{
	int ret;
	char node[64];

	snprintf(node, sizeof(node), "/dev/ubi%d_%d",
		vol->dev_num,
		vol->vol_id);

	ret = mount(node, mntpoint,
                 "ubifs", 0, NULL);

	if (ret)
		ERROR("UBIFS cannot be mounted : device %s volume %s on %s : %s",
			node, vol->name, mntpoint, strerror(errno));
}

void ubi_umount(const char *mntpoint)
{
	umount(mntpoint);
}

void mtd_cleanup (void)
{
	int i;
	struct ubilist *list;
	struct ubi_part *vol, *tmp;
	struct flash_description *flash = get_flash_info();

	if (flash->mtd_info) {
		for (i = flash->mtd.lowest_mtd_num; i <= flash->mtd.highest_mtd_num; i++) {
			list = &flash->mtd_info[i].ubi_partitions;
			LIST_FOREACH_SAFE(vol, list, next, tmp) {
				LIST_REMOVE(vol, next);
				free(vol);
			}
		}
		free(flash->mtd_info);
		flash->mtd_info = NULL;
	}

	/* Do not clear libraries handles */
	memset(&flash->ubi_info, 0, sizeof(struct ubi_info));
	memset(&flash->mtd, 0, sizeof(struct mtd_info));
}