jffs2.c 15 KB

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