cmd_jffs2.c 16 KB

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