jffs2.c 15 KB

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