cmd_jffs2.c 16 KB

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