cmd_jffs2.c 16 KB

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