jffs2.c 15 KB

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