jffs2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. #if defined(CONFIG_JFFS2_DEV)
  320. dev_name = CONFIG_JFFS2_DEV;
  321. #else
  322. dev_name = "nor0";
  323. #endif
  324. if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
  325. (mtd_device_validate(id->type, id->num, &size) != 0)) {
  326. printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
  327. free(current_mtd_dev);
  328. return 1;
  329. }
  330. id->size = size;
  331. INIT_LIST_HEAD(&id->link);
  332. DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
  333. id->type, id->num, id->size, id->mtd_id);
  334. /* partition */
  335. part->name = "static";
  336. part->auto_name = 0;
  337. #if defined(CONFIG_JFFS2_PART_SIZE)
  338. part->size = CONFIG_JFFS2_PART_SIZE;
  339. #else
  340. part->size = SIZE_REMAINING;
  341. #endif
  342. #if defined(CONFIG_JFFS2_PART_OFFSET)
  343. part->offset = CONFIG_JFFS2_PART_OFFSET;
  344. #else
  345. part->offset = 0x00000000;
  346. #endif
  347. part->dev = current_mtd_dev;
  348. INIT_LIST_HEAD(&part->link);
  349. /* recalculate size if needed */
  350. if (part->size == SIZE_REMAINING)
  351. part->size = id->size - part->offset;
  352. part->sector_size = get_part_sector_size(id, part);
  353. DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
  354. part->name, part->size, part->offset);
  355. /* device */
  356. current_mtd_dev->id = id;
  357. INIT_LIST_HEAD(&current_mtd_dev->link);
  358. current_mtd_dev->num_parts = 1;
  359. INIT_LIST_HEAD(&current_mtd_dev->parts);
  360. list_add(&part->link, &current_mtd_dev->parts);
  361. }
  362. return 0;
  363. }
  364. #endif /* #ifndef CONFIG_CMD_MTDPARTS */
  365. /**
  366. * Return pointer to the partition of a requested number from a requested
  367. * device.
  368. *
  369. * @param dev device that is to be searched for a partition
  370. * @param part_num requested partition number
  371. * @return pointer to the part_info, NULL otherwise
  372. */
  373. static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
  374. {
  375. struct list_head *entry;
  376. struct part_info *part;
  377. int num;
  378. if (!dev)
  379. return NULL;
  380. DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
  381. part_num, MTD_DEV_TYPE(dev->id->type),
  382. dev->id->num, dev->id->mtd_id);
  383. if (part_num >= dev->num_parts) {
  384. printf("invalid partition number %d for device %s%d (%s)\n",
  385. part_num, MTD_DEV_TYPE(dev->id->type),
  386. dev->id->num, dev->id->mtd_id);
  387. return NULL;
  388. }
  389. /* locate partition number, return it */
  390. num = 0;
  391. list_for_each(entry, &dev->parts) {
  392. part = list_entry(entry, struct part_info, link);
  393. if (part_num == num++) {
  394. return part;
  395. }
  396. }
  397. return NULL;
  398. }
  399. /***************************************************/
  400. /* U-Boot commands */
  401. /***************************************************/
  402. /**
  403. * Routine implementing fsload u-boot command. This routine tries to load
  404. * a requested file from jffs2/cramfs filesystem on a current partition.
  405. *
  406. * @param cmdtp command internal data
  407. * @param flag command flag
  408. * @param argc number of arguments supplied to the command
  409. * @param argv arguments list
  410. * @return 0 on success, 1 otherwise
  411. */
  412. int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
  413. char *const argv[])
  414. {
  415. char *fsname;
  416. char *filename;
  417. int size;
  418. struct part_info *part;
  419. ulong offset = image_load_addr;
  420. /* pre-set Boot file name */
  421. filename = env_get("bootfile");
  422. if (!filename)
  423. filename = "uImage";
  424. if (argc == 2) {
  425. filename = argv[1];
  426. }
  427. if (argc == 3) {
  428. offset = hextoul(argv[1], NULL);
  429. image_load_addr = offset;
  430. filename = argv[2];
  431. }
  432. /* make sure we are in sync with env variables */
  433. if (mtdparts_init() !=0)
  434. return 1;
  435. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  436. /* check partition type for cramfs */
  437. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  438. printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
  439. if (cramfs_check(part)) {
  440. size = cramfs_load ((char *) offset, part, filename);
  441. } else {
  442. /* if this is not cramfs assume jffs2 */
  443. size = jffs2_1pass_load((char *)offset, part, filename);
  444. }
  445. if (size > 0) {
  446. printf("### %s load complete: %d bytes loaded to 0x%lx\n",
  447. fsname, size, offset);
  448. env_set_hex("filesize", size);
  449. } else {
  450. printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
  451. }
  452. return !(size > 0);
  453. }
  454. return 1;
  455. }
  456. /**
  457. * Routine implementing u-boot ls command which lists content of a given
  458. * directory on a current partition.
  459. *
  460. * @param cmdtp command internal data
  461. * @param flag command flag
  462. * @param argc number of arguments supplied to the command
  463. * @param argv arguments list
  464. * @return 0 on success, 1 otherwise
  465. */
  466. int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  467. {
  468. char *filename = "/";
  469. int ret;
  470. struct part_info *part;
  471. if (argc == 2)
  472. filename = argv[1];
  473. /* make sure we are in sync with env variables */
  474. if (mtdparts_init() !=0)
  475. return 1;
  476. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  477. /* check partition type for cramfs */
  478. if (cramfs_check(part)) {
  479. ret = cramfs_ls (part, filename);
  480. } else {
  481. /* if this is not cramfs assume jffs2 */
  482. ret = jffs2_1pass_ls(part, filename);
  483. }
  484. return ret ? 0 : 1;
  485. }
  486. return 1;
  487. }
  488. /**
  489. * Routine implementing u-boot fsinfo command. This routine prints out
  490. * miscellaneous filesystem informations/statistics.
  491. *
  492. * @param cmdtp command internal data
  493. * @param flag command flag
  494. * @param argc number of arguments supplied to the command
  495. * @param argv arguments list
  496. * @return 0 on success, 1 otherwise
  497. */
  498. int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
  499. char *const argv[])
  500. {
  501. struct part_info *part;
  502. char *fsname;
  503. int ret;
  504. /* make sure we are in sync with env variables */
  505. if (mtdparts_init() !=0)
  506. return 1;
  507. if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
  508. /* check partition type for cramfs */
  509. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  510. printf("### filesystem type is %s\n", fsname);
  511. if (cramfs_check(part)) {
  512. ret = cramfs_info (part);
  513. } else {
  514. /* if this is not cramfs assume jffs2 */
  515. ret = jffs2_1pass_info(part);
  516. }
  517. return ret ? 0 : 1;
  518. }
  519. return 1;
  520. }
  521. /***************************************************/
  522. U_BOOT_CMD(
  523. fsload, 3, 0, do_jffs2_fsload,
  524. "load binary file from a filesystem image",
  525. "[ off ] [ filename ]\n"
  526. " - load binary file from flash bank\n"
  527. " with offset 'off'"
  528. );
  529. U_BOOT_CMD(
  530. fsls, 2, 1, do_jffs2_ls,
  531. "list files in a directory (default /)",
  532. "[ directory ]"
  533. );
  534. U_BOOT_CMD(
  535. fsinfo, 1, 1, do_jffs2_fsinfo,
  536. "print information about filesystems",
  537. ""
  538. );
  539. /***************************************************/