jffs2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2002
  7. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  8. *
  9. * (C) Copyright 2003
  10. * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
  11. *
  12. * (C) Copyright 2005
  13. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  14. *
  15. * Added support for reading flash partition table from environment.
  16. * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
  17. * kernel tree.
  18. *
  19. * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
  20. * Copyright 2002 SYSGO Real-Time Solutions GmbH
  21. */
  22. /*
  23. * Three environment variables are used by the parsing routines:
  24. *
  25. * 'partition' - keeps current partition identifier
  26. *
  27. * partition := <part-id>
  28. * <part-id> := <dev-id>,part_num
  29. *
  30. *
  31. * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
  32. *
  33. * mtdids=<idmap>[,<idmap>,...]
  34. *
  35. * <idmap> := <dev-id>=<mtd-id>
  36. * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
  37. * <dev-num> := mtd device number, 0...
  38. * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
  39. *
  40. *
  41. * 'mtdparts' - partition list
  42. *
  43. * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
  44. *
  45. * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
  46. * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
  47. * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
  48. * <size> := standard linux memsize OR '-' to denote all remaining space
  49. * <offset> := partition start offset within the device
  50. * <name> := '(' NAME ')'
  51. * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
  52. *
  53. * Notes:
  54. * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
  55. * - if the above variables are not set defaults for a given target are used
  56. *
  57. * Examples:
  58. *
  59. * 1 NOR Flash, with 1 single writable partition:
  60. * mtdids=nor0=edb7312-nor
  61. * mtdparts=mtdparts=edb7312-nor:-
  62. *
  63. * 1 NOR Flash with 2 partitions, 1 NAND with one
  64. * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
  65. * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  66. *
  67. */
  68. /*
  69. * JFFS2/CRAMFS support
  70. */
  71. #include <common.h>
  72. #include <command.h>
  73. #include <env.h>
  74. #include <flash.h>
  75. #include <image.h>
  76. #include <malloc.h>
  77. #include <jffs2/jffs2.h>
  78. #include <linux/bug.h>
  79. #include <linux/list.h>
  80. #include <linux/ctype.h>
  81. #include <cramfs/cramfs_fs.h>
  82. #if defined(CONFIG_CMD_NAND)
  83. #include <linux/mtd/rawnand.h>
  84. #include <nand.h>
  85. #endif
  86. #if defined(CONFIG_CMD_ONENAND)
  87. #include <linux/mtd/mtd.h>
  88. #include <linux/mtd/onenand.h>
  89. #include <onenand_uboot.h>
  90. #endif
  91. /* enable/disable debugging messages */
  92. #define DEBUG_JFFS
  93. #undef DEBUG_JFFS
  94. #ifdef DEBUG_JFFS
  95. # define DEBUGF(fmt, args...) printf(fmt ,##args)
  96. #else
  97. # define DEBUGF(fmt, args...)
  98. #endif
  99. /* special size referring to all the remaining space in a partition */
  100. #define SIZE_REMAINING 0xFFFFFFFF
  101. /* special offset value, it is used when not provided by user
  102. *
  103. * this value is used temporarily during parsing, later such offests
  104. * are recalculated */
  105. #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
  106. /* minimum partition size */
  107. #define MIN_PART_SIZE 4096
  108. /* this flag needs to be set in part_info struct mask_flags
  109. * field for read-only partitions */
  110. #define MTD_WRITEABLE_CMD 1
  111. /* current active device and partition number */
  112. #ifdef CONFIG_CMD_MTDPARTS
  113. /* Use the ones declared in cmd_mtdparts.c */
  114. extern struct mtd_device *current_mtd_dev;
  115. extern u8 current_mtd_partnum;
  116. #else
  117. /* Use local ones */
  118. struct mtd_device *current_mtd_dev = NULL;
  119. u8 current_mtd_partnum = 0;
  120. #endif
  121. #if defined(CONFIG_CMD_CRAMFS)
  122. extern int cramfs_check (struct part_info *info);
  123. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  124. extern int cramfs_ls (struct part_info *info, char *filename);
  125. extern int cramfs_info (struct part_info *info);
  126. #else
  127. /* defining empty macros for function names is ugly but avoids ifdef clutter
  128. * all over the code */
  129. #define cramfs_check(x) (0)
  130. #define cramfs_load(x,y,z) (-1)
  131. #define cramfs_ls(x,y) (0)
  132. #define cramfs_info(x) (0)
  133. #endif
  134. #ifndef CONFIG_CMD_MTDPARTS
  135. /**
  136. * Check device number to be within valid range for given device type.
  137. *
  138. * @param dev device to validate
  139. * Return: 0 if device is valid, 1 otherwise
  140. */
  141. static int mtd_device_validate(u8 type, u8 num, u32 *size)
  142. {
  143. if (type == MTD_DEV_TYPE_NOR) {
  144. #if defined(CONFIG_CMD_FLASH)
  145. if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
  146. extern flash_info_t flash_info[];
  147. *size = flash_info[num].size;
  148. return 0;
  149. }
  150. printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
  151. MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
  152. #else
  153. printf("support for FLASH devices not present\n");
  154. #endif
  155. } else if (type == MTD_DEV_TYPE_NAND) {
  156. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  157. struct mtd_info *mtd = get_nand_dev_by_index(num);
  158. if (mtd) {
  159. *size = mtd->size;
  160. return 0;
  161. }
  162. printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
  163. MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
  164. #else
  165. printf("support for NAND devices not present\n");
  166. #endif
  167. } else if (type == MTD_DEV_TYPE_ONENAND) {
  168. #if defined(CONFIG_CMD_ONENAND)
  169. *size = onenand_mtd.size;
  170. return 0;
  171. #else
  172. printf("support for OneNAND devices not present\n");
  173. #endif
  174. } else
  175. printf("Unknown defice type %d\n", type);
  176. return 1;
  177. }
  178. /**
  179. * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
  180. * return device type and number.
  181. *
  182. * @param id string describing device id
  183. * @param ret_id output pointer to next char after parse completes (output)
  184. * @param dev_type parsed device type (output)
  185. * @param dev_num parsed device number (output)
  186. * Return: 0 on success, 1 otherwise
  187. */
  188. static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
  189. {
  190. const char *p = id;
  191. *dev_type = 0;
  192. if (strncmp(p, "nand", 4) == 0) {
  193. *dev_type = MTD_DEV_TYPE_NAND;
  194. p += 4;
  195. } else if (strncmp(p, "nor", 3) == 0) {
  196. *dev_type = MTD_DEV_TYPE_NOR;
  197. p += 3;
  198. } else if (strncmp(p, "onenand", 7) == 0) {
  199. *dev_type = MTD_DEV_TYPE_ONENAND;
  200. p += 7;
  201. } else {
  202. printf("incorrect device type in %s\n", id);
  203. return 1;
  204. }
  205. if (!isdigit(*p)) {
  206. printf("incorrect device number in %s\n", id);
  207. return 1;
  208. }
  209. *dev_num = simple_strtoul(p, (char **)&p, 0);
  210. if (ret_id)
  211. *ret_id = p;
  212. return 0;
  213. }
  214. /*
  215. * 'Static' version of command line mtdparts_init() routine. Single partition on
  216. * a single device configuration.
  217. */
  218. /**
  219. * Calculate sector size.
  220. *
  221. * Return: sector size
  222. */
  223. static inline u32 get_part_sector_size_nand(struct mtdids *id)
  224. {
  225. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  226. struct mtd_info *mtd;
  227. mtd = get_nand_dev_by_index(id->num);
  228. return mtd->erasesize;
  229. #else
  230. BUG();
  231. return 0;
  232. #endif
  233. }
  234. static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
  235. {
  236. #if defined(CONFIG_CMD_FLASH)
  237. extern flash_info_t flash_info[];
  238. u32 end_phys, start_phys, sector_size = 0, size = 0;
  239. int i;
  240. flash_info_t *flash;
  241. flash = &flash_info[id->num];
  242. start_phys = flash->start[0] + part->offset;
  243. end_phys = start_phys + part->size - 1;
  244. for (i = 0; i < flash->sector_count; i++) {
  245. if (flash->start[i] >= end_phys)
  246. break;
  247. if (flash->start[i] >= start_phys) {
  248. if (i == flash->sector_count - 1) {
  249. size = flash->start[0] + flash->size - flash->start[i];
  250. } else {
  251. size = flash->start[i+1] - flash->start[i];
  252. }
  253. if (sector_size < size)
  254. sector_size = size;
  255. }
  256. }
  257. return sector_size;
  258. #else
  259. BUG();
  260. return 0;
  261. #endif
  262. }
  263. static inline u32 get_part_sector_size_onenand(void)
  264. {
  265. #if defined(CONFIG_CMD_ONENAND)
  266. struct mtd_info *mtd;
  267. mtd = &onenand_mtd;
  268. return mtd->erasesize;
  269. #else
  270. BUG();
  271. return 0;
  272. #endif
  273. }
  274. static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
  275. {
  276. if (id->type == MTD_DEV_TYPE_NAND)
  277. return get_part_sector_size_nand(id);
  278. else if (id->type == MTD_DEV_TYPE_NOR)
  279. return get_part_sector_size_nor(id, part);
  280. else if (id->type == MTD_DEV_TYPE_ONENAND)
  281. return get_part_sector_size_onenand();
  282. else
  283. DEBUGF("Error: Unknown device type.\n");
  284. return 0;
  285. }
  286. /**
  287. * Parse and initialize global mtdids mapping and create global
  288. * device/partition list.
  289. *
  290. * 'Static' version of command line mtdparts_init() routine. Single partition on
  291. * a single device configuration.
  292. *
  293. * Return: 0 on success, 1 otherwise
  294. */
  295. int mtdparts_init(void)
  296. {
  297. static int initialized = 0;
  298. u32 size;
  299. char *dev_name;
  300. DEBUGF("\n---mtdparts_init---\n");
  301. if (!initialized) {
  302. struct mtdids *id;
  303. struct part_info *part;
  304. initialized = 1;
  305. current_mtd_dev = (struct mtd_device *)
  306. malloc(sizeof(struct mtd_device) +
  307. sizeof(struct part_info) +
  308. sizeof(struct mtdids));
  309. if (!current_mtd_dev) {
  310. printf("out of memory\n");
  311. return 1;
  312. }
  313. memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
  314. sizeof(struct part_info) + sizeof(struct mtdids));
  315. id = (struct mtdids *)(current_mtd_dev + 1);
  316. part = (struct part_info *)(id + 1);
  317. /* id */
  318. id->mtd_id = "single part";
  319. dev_name = CONFIG_JFFS2_DEV;
  320. if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
  321. (mtd_device_validate(id->type, id->num, &size) != 0)) {
  322. printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
  323. free(current_mtd_dev);
  324. return 1;
  325. }
  326. id->size = size;
  327. INIT_LIST_HEAD(&id->link);
  328. DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
  329. id->type, id->num, id->size, id->mtd_id);
  330. /* partition */
  331. part->name = "static";
  332. part->auto_name = 0;
  333. part->size = CONFIG_JFFS2_PART_SIZE;
  334. part->offset = CONFIG_JFFS2_PART_OFFSET;
  335. part->dev = current_mtd_dev;
  336. INIT_LIST_HEAD(&part->link);
  337. /* recalculate size if needed */
  338. if (part->size == SIZE_REMAINING)
  339. part->size = id->size - part->offset;
  340. part->sector_size = get_part_sector_size(id, part);
  341. DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
  342. part->name, part->size, part->offset);
  343. /* device */
  344. current_mtd_dev->id = id;
  345. INIT_LIST_HEAD(&current_mtd_dev->link);
  346. current_mtd_dev->num_parts = 1;
  347. INIT_LIST_HEAD(&current_mtd_dev->parts);
  348. list_add(&part->link, &current_mtd_dev->parts);
  349. }
  350. return 0;
  351. }
  352. #endif /* #ifndef CONFIG_CMD_MTDPARTS */
  353. /**
  354. * Return pointer to the partition of a requested number from a requested
  355. * device.
  356. *
  357. * @param dev device that is to be searched for a partition
  358. * @param part_num requested partition number
  359. * Return: pointer to the part_info, NULL otherwise
  360. */
  361. static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
  362. {
  363. struct list_head *entry;
  364. struct part_info *part;
  365. int num;
  366. if (!dev)
  367. return NULL;
  368. DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
  369. part_num, MTD_DEV_TYPE(dev->id->type),
  370. dev->id->num, dev->id->mtd_id);
  371. if (part_num >= dev->num_parts) {
  372. printf("invalid partition number %d for device %s%d (%s)\n",
  373. part_num, MTD_DEV_TYPE(dev->id->type),
  374. dev->id->num, dev->id->mtd_id);
  375. return NULL;
  376. }
  377. /* locate partition number, return it */
  378. num = 0;
  379. list_for_each(entry, &dev->parts) {
  380. part = list_entry(entry, struct part_info, link);
  381. if (part_num == num++) {
  382. return part;
  383. }
  384. }
  385. return NULL;
  386. }
  387. /***************************************************/
  388. /* U-Boot commands */
  389. /***************************************************/
  390. /**
  391. * Routine implementing fsload u-boot command. This routine tries to load
  392. * a requested file from jffs2/cramfs filesystem on a current partition.
  393. *
  394. * @param cmdtp command internal data
  395. * @param flag command flag
  396. * @param argc number of arguments supplied to the command
  397. * @param argv arguments list
  398. * Return: 0 on success, 1 otherwise
  399. */
  400. int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
  401. char *const argv[])
  402. {
  403. char *fsname;
  404. char *filename;
  405. int size;
  406. struct part_info *part;
  407. ulong offset = image_load_addr;
  408. /* pre-set Boot file name */
  409. filename = env_get("bootfile");
  410. if (!filename)
  411. filename = "uImage";
  412. if (argc == 2) {
  413. filename = argv[1];
  414. }
  415. if (argc == 3) {
  416. offset = hextoul(argv[1], NULL);
  417. image_load_addr = offset;
  418. filename = argv[2];
  419. }
  420. /* make sure we are in sync with env variables */
  421. if (mtdparts_init() !=0)
  422. return 1;
  423. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  424. /* check partition type for cramfs */
  425. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  426. printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
  427. if (cramfs_check(part)) {
  428. size = cramfs_load ((char *) offset, part, filename);
  429. } else {
  430. /* if this is not cramfs assume jffs2 */
  431. size = jffs2_1pass_load((char *)offset, part, filename);
  432. }
  433. if (size > 0) {
  434. printf("### %s load complete: %d bytes loaded to 0x%lx\n",
  435. fsname, size, offset);
  436. env_set_hex("filesize", size);
  437. } else {
  438. printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
  439. }
  440. return !(size > 0);
  441. }
  442. return 1;
  443. }
  444. /**
  445. * Routine implementing u-boot ls command which lists content of a given
  446. * directory on a current partition.
  447. *
  448. * @param cmdtp command internal data
  449. * @param flag command flag
  450. * @param argc number of arguments supplied to the command
  451. * @param argv arguments list
  452. * Return: 0 on success, 1 otherwise
  453. */
  454. int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  455. {
  456. char *filename = "/";
  457. int ret;
  458. struct part_info *part;
  459. if (argc == 2)
  460. filename = argv[1];
  461. /* make sure we are in sync with env variables */
  462. if (mtdparts_init() !=0)
  463. return 1;
  464. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  465. /* check partition type for cramfs */
  466. if (cramfs_check(part)) {
  467. ret = cramfs_ls (part, filename);
  468. } else {
  469. /* if this is not cramfs assume jffs2 */
  470. ret = jffs2_1pass_ls(part, filename);
  471. }
  472. return ret ? 0 : 1;
  473. }
  474. return 1;
  475. }
  476. /**
  477. * Routine implementing u-boot fsinfo command. This routine prints out
  478. * miscellaneous filesystem informations/statistics.
  479. *
  480. * @param cmdtp command internal data
  481. * @param flag command flag
  482. * @param argc number of arguments supplied to the command
  483. * @param argv arguments list
  484. * Return: 0 on success, 1 otherwise
  485. */
  486. int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
  487. char *const argv[])
  488. {
  489. struct part_info *part;
  490. char *fsname;
  491. int ret;
  492. /* make sure we are in sync with env variables */
  493. if (mtdparts_init() !=0)
  494. return 1;
  495. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  496. /* check partition type for cramfs */
  497. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  498. printf("### filesystem type is %s\n", fsname);
  499. if (cramfs_check(part)) {
  500. ret = cramfs_info (part);
  501. } else {
  502. /* if this is not cramfs assume jffs2 */
  503. ret = jffs2_1pass_info(part);
  504. }
  505. return ret ? 0 : 1;
  506. }
  507. return 1;
  508. }
  509. /***************************************************/
  510. U_BOOT_CMD(
  511. fsload, 3, 0, do_jffs2_fsload,
  512. "load binary file from a filesystem image",
  513. "[ off ] [ filename ]\n"
  514. " - load binary file from flash bank\n"
  515. " with offset 'off'"
  516. );
  517. U_BOOT_CMD(
  518. fsls, 2, 1, do_jffs2_ls,
  519. "list files in a directory (default /)",
  520. "[ directory ]"
  521. );
  522. U_BOOT_CMD(
  523. fsinfo, 1, 1, do_jffs2_fsinfo,
  524. "print information about filesystems",
  525. ""
  526. );
  527. /***************************************************/