jffs2.c 15 KB

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