cmd_mtdparts.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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_MTDPARTS
  110. #undef DEBUG_MTDPARTS
  111. #ifdef DEBUG_MTDPARTS
  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. /* default values for mtdids and mtdparts variables */
  129. #if defined(MTDIDS_DEFAULT)
  130. static const char *const mtdids_default = MTDIDS_DEFAULT;
  131. #else
  132. #warning "MTDIDS_DEFAULT not defined!"
  133. static const char *const mtdids_default = NULL;
  134. #endif
  135. #if defined(MTDPARTS_DEFAULT)
  136. static const char *const mtdparts_default = MTDPARTS_DEFAULT;
  137. #else
  138. #warning "MTDPARTS_DEFAULT not defined!"
  139. static const char *const mtdparts_default = NULL;
  140. #endif
  141. /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
  142. #define MTDIDS_MAXLEN 128
  143. #define MTDPARTS_MAXLEN 512
  144. #define PARTITION_MAXLEN 16
  145. static char last_ids[MTDIDS_MAXLEN];
  146. static char last_parts[MTDPARTS_MAXLEN];
  147. static char last_partition[PARTITION_MAXLEN];
  148. /* low level jffs2 cache cleaning routine */
  149. extern void jffs2_free_cache(struct part_info *part);
  150. /* mtdids mapping list, filled by parse_ids() */
  151. struct list_head mtdids;
  152. /* device/partition list, parse_cmdline() parses into here */
  153. struct list_head devices;
  154. /* current active device and partition number */
  155. static struct mtd_device *current_dev = NULL;
  156. static u8 current_partnum = 0;
  157. static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
  158. /* command line only routines */
  159. static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
  160. static int device_del(struct mtd_device *dev);
  161. /**
  162. * Parses a string into a number. The number stored at ptr is
  163. * potentially suffixed with K (for kilobytes, or 1024 bytes),
  164. * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
  165. * 1073741824). If the number is suffixed with K, M, or G, then
  166. * the return value is the number multiplied by one kilobyte, one
  167. * megabyte, or one gigabyte, respectively.
  168. *
  169. * @param ptr where parse begins
  170. * @param retptr output pointer to next char after parse completes (output)
  171. * @return resulting unsigned int
  172. */
  173. static unsigned long memsize_parse (const char *const ptr, const char **retptr)
  174. {
  175. unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
  176. switch (**retptr) {
  177. case 'G':
  178. case 'g':
  179. ret <<= 10;
  180. case 'M':
  181. case 'm':
  182. ret <<= 10;
  183. case 'K':
  184. case 'k':
  185. ret <<= 10;
  186. (*retptr)++;
  187. default:
  188. break;
  189. }
  190. return ret;
  191. }
  192. /**
  193. * Format string describing supplied size. This routine does the opposite job
  194. * to memsize_parse(). Size in bytes is converted to string and if possible
  195. * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
  196. *
  197. * Note, that this routine does not check for buffer overflow, it's the caller
  198. * who must assure enough space.
  199. *
  200. * @param buf output buffer
  201. * @param size size to be converted to string
  202. */
  203. static void memsize_format(char *buf, u32 size)
  204. {
  205. #define SIZE_GB ((u32)1024*1024*1024)
  206. #define SIZE_MB ((u32)1024*1024)
  207. #define SIZE_KB ((u32)1024)
  208. if ((size % SIZE_GB) == 0)
  209. sprintf(buf, "%ug", size/SIZE_GB);
  210. else if ((size % SIZE_MB) == 0)
  211. sprintf(buf, "%um", size/SIZE_MB);
  212. else if (size % SIZE_KB == 0)
  213. sprintf(buf, "%uk", size/SIZE_KB);
  214. else
  215. sprintf(buf, "%u", size);
  216. }
  217. /**
  218. * This routine does global indexing of all partitions. Resulting index for
  219. * current partition is saved in 'mtddevnum'. Current partition name in
  220. * 'mtddevname'.
  221. */
  222. static void index_partitions(void)
  223. {
  224. char buf[16];
  225. u16 mtddevnum;
  226. struct part_info *part;
  227. struct list_head *dentry;
  228. struct mtd_device *dev;
  229. DEBUGF("--- index partitions ---\n");
  230. if (current_dev) {
  231. mtddevnum = 0;
  232. list_for_each(dentry, &devices) {
  233. dev = list_entry(dentry, struct mtd_device, link);
  234. if (dev == current_dev) {
  235. mtddevnum += current_partnum;
  236. sprintf(buf, "%d", mtddevnum);
  237. setenv("mtddevnum", buf);
  238. break;
  239. }
  240. mtddevnum += dev->num_parts;
  241. }
  242. part = mtd_part_info(current_dev, current_partnum);
  243. setenv("mtddevname", part->name);
  244. DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
  245. } else {
  246. setenv("mtddevnum", NULL);
  247. setenv("mtddevname", NULL);
  248. DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
  249. }
  250. }
  251. /**
  252. * Save current device and partition in environment variable 'partition'.
  253. */
  254. static void current_save(void)
  255. {
  256. char buf[16];
  257. DEBUGF("--- current_save ---\n");
  258. if (current_dev) {
  259. sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
  260. current_dev->id->num, current_partnum);
  261. setenv("partition", buf);
  262. strncpy(last_partition, buf, 16);
  263. DEBUGF("=> partition %s\n", buf);
  264. } else {
  265. setenv("partition", NULL);
  266. last_partition[0] = '\0';
  267. DEBUGF("=> partition NULL\n");
  268. }
  269. index_partitions();
  270. }
  271. /**
  272. * Performs sanity check for supplied NOR flash partition. Table of existing
  273. * NOR flash devices is searched and partition device is located. Alignment
  274. * with the granularity of NOR flash sectors is verified.
  275. *
  276. * @param id of the parent device
  277. * @param part partition to validate
  278. * @return 0 if partition is valid, 1 otherwise
  279. */
  280. static int part_validate_nor(struct mtdids *id, struct part_info *part)
  281. {
  282. #if defined(CONFIG_CMD_FLASH)
  283. /* info for FLASH chips */
  284. extern flash_info_t flash_info[];
  285. flash_info_t *flash;
  286. int offset_aligned;
  287. u32 end_offset, sector_size = 0;
  288. int i;
  289. flash = &flash_info[id->num];
  290. /* size of last sector */
  291. part->sector_size = flash->size -
  292. (flash->start[flash->sector_count-1] - flash->start[0]);
  293. offset_aligned = 0;
  294. for (i = 0; i < flash->sector_count; i++) {
  295. if ((flash->start[i] - flash->start[0]) == part->offset) {
  296. offset_aligned = 1;
  297. break;
  298. }
  299. }
  300. if (offset_aligned == 0) {
  301. printf("%s%d: partition (%s) start offset alignment incorrect\n",
  302. MTD_DEV_TYPE(id->type), id->num, part->name);
  303. return 1;
  304. }
  305. end_offset = part->offset + part->size;
  306. offset_aligned = 0;
  307. for (i = 0; i < flash->sector_count; i++) {
  308. if (i) {
  309. sector_size = flash->start[i] - flash->start[i-1];
  310. if (part->sector_size < sector_size)
  311. part->sector_size = sector_size;
  312. }
  313. if ((flash->start[i] - flash->start[0]) == end_offset)
  314. offset_aligned = 1;
  315. }
  316. if (offset_aligned || flash->size == end_offset)
  317. return 0;
  318. printf("%s%d: partition (%s) size alignment incorrect\n",
  319. MTD_DEV_TYPE(id->type), id->num, part->name);
  320. #endif
  321. return 1;
  322. }
  323. /**
  324. * Performs sanity check for supplied NAND flash partition. Table of existing
  325. * NAND flash devices is searched and partition device is located. Alignment
  326. * with the granularity of nand erasesize is verified.
  327. *
  328. * @param id of the parent device
  329. * @param part partition to validate
  330. * @return 0 if partition is valid, 1 otherwise
  331. */
  332. static int part_validate_nand(struct mtdids *id, struct part_info *part)
  333. {
  334. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  335. /* info for NAND chips */
  336. nand_info_t *nand;
  337. nand = &nand_info[id->num];
  338. part->sector_size = nand->erasesize;
  339. if ((unsigned long)(part->offset) % nand->erasesize) {
  340. printf("%s%d: partition (%s) start offset alignment incorrect\n",
  341. MTD_DEV_TYPE(id->type), id->num, part->name);
  342. return 1;
  343. }
  344. if (part->size % nand->erasesize) {
  345. printf("%s%d: partition (%s) size alignment incorrect\n",
  346. MTD_DEV_TYPE(id->type), id->num, part->name);
  347. return 1;
  348. }
  349. return 0;
  350. #else
  351. return 1;
  352. #endif
  353. }
  354. /**
  355. * Performs sanity check for supplied OneNAND flash partition.
  356. * Table of existing OneNAND flash devices is searched and partition device
  357. * is located. Alignment with the granularity of nand erasesize is verified.
  358. *
  359. * @param id of the parent device
  360. * @param part partition to validate
  361. * @return 0 if partition is valid, 1 otherwise
  362. */
  363. static int part_validate_onenand(struct mtdids *id, struct part_info *part)
  364. {
  365. #if defined(CONFIG_CMD_ONENAND)
  366. /* info for OneNAND chips */
  367. struct mtd_info *mtd;
  368. mtd = &onenand_mtd;
  369. part->sector_size = mtd->erasesize;
  370. if ((unsigned long)(part->offset) % mtd->erasesize) {
  371. printf("%s%d: partition (%s) start offset"
  372. "alignment incorrect\n",
  373. MTD_DEV_TYPE(id->type), id->num, part->name);
  374. return 1;
  375. }
  376. if (part->size % mtd->erasesize) {
  377. printf("%s%d: partition (%s) size alignment incorrect\n",
  378. MTD_DEV_TYPE(id->type), id->num, part->name);
  379. return 1;
  380. }
  381. return 0;
  382. #else
  383. return 1;
  384. #endif
  385. }
  386. /**
  387. * Performs sanity check for supplied partition. Offset and size are verified
  388. * to be within valid range. Partition type is checked and either
  389. * parts_validate_nor() or parts_validate_nand() is called with the argument
  390. * of part.
  391. *
  392. * @param id of the parent device
  393. * @param part partition to validate
  394. * @return 0 if partition is valid, 1 otherwise
  395. */
  396. static int part_validate(struct mtdids *id, struct part_info *part)
  397. {
  398. if (part->size == SIZE_REMAINING)
  399. part->size = id->size - part->offset;
  400. if (part->offset > id->size) {
  401. printf("%s: offset %08x beyond flash size %08x\n",
  402. id->mtd_id, part->offset, id->size);
  403. return 1;
  404. }
  405. if ((part->offset + part->size) <= part->offset) {
  406. printf("%s%d: partition (%s) size too big\n",
  407. MTD_DEV_TYPE(id->type), id->num, part->name);
  408. return 1;
  409. }
  410. if (part->offset + part->size > id->size) {
  411. printf("%s: partitioning exceeds flash size\n", id->mtd_id);
  412. return 1;
  413. }
  414. if (id->type == MTD_DEV_TYPE_NAND)
  415. return part_validate_nand(id, part);
  416. else if (id->type == MTD_DEV_TYPE_NOR)
  417. return part_validate_nor(id, part);
  418. else if (id->type == MTD_DEV_TYPE_ONENAND)
  419. return part_validate_onenand(id, part);
  420. else
  421. DEBUGF("part_validate: invalid dev type\n");
  422. return 1;
  423. }
  424. /**
  425. * Delete selected partition from the partion list of the specified device.
  426. *
  427. * @param dev device to delete partition from
  428. * @param part partition to delete
  429. * @return 0 on success, 1 otherwise
  430. */
  431. static int part_del(struct mtd_device *dev, struct part_info *part)
  432. {
  433. u8 current_save_needed = 0;
  434. /* if there is only one partition, remove whole device */
  435. if (dev->num_parts == 1)
  436. return device_del(dev);
  437. /* otherwise just delete this partition */
  438. if (dev == current_dev) {
  439. /* we are modyfing partitions for the current device,
  440. * update current */
  441. struct part_info *curr_pi;
  442. curr_pi = mtd_part_info(current_dev, current_partnum);
  443. if (curr_pi) {
  444. if (curr_pi == part) {
  445. printf("current partition deleted, resetting current to 0\n");
  446. current_partnum = 0;
  447. } else if (part->offset <= curr_pi->offset) {
  448. current_partnum--;
  449. }
  450. current_save_needed = 1;
  451. }
  452. }
  453. #ifdef CONFIG_NAND_LEGACY
  454. jffs2_free_cache(part);
  455. #endif
  456. list_del(&part->link);
  457. free(part);
  458. dev->num_parts--;
  459. if (current_save_needed > 0)
  460. current_save();
  461. else
  462. index_partitions();
  463. return 0;
  464. }
  465. /**
  466. * Delete all partitions from parts head list, free memory.
  467. *
  468. * @param head list of partitions to delete
  469. */
  470. static void part_delall(struct list_head *head)
  471. {
  472. struct list_head *entry, *n;
  473. struct part_info *part_tmp;
  474. /* clean tmp_list and free allocated memory */
  475. list_for_each_safe(entry, n, head) {
  476. part_tmp = list_entry(entry, struct part_info, link);
  477. #ifdef CONFIG_NAND_LEGACY
  478. jffs2_free_cache(part_tmp);
  479. #endif
  480. list_del(entry);
  481. free(part_tmp);
  482. }
  483. }
  484. /**
  485. * Add new partition to the supplied partition list. Make sure partitions are
  486. * sorted by offset in ascending order.
  487. *
  488. * @param head list this partition is to be added to
  489. * @param new partition to be added
  490. */
  491. static int part_sort_add(struct mtd_device *dev, struct part_info *part)
  492. {
  493. struct list_head *entry;
  494. struct part_info *new_pi, *curr_pi;
  495. /* link partition to parrent dev */
  496. part->dev = dev;
  497. if (list_empty(&dev->parts)) {
  498. DEBUGF("part_sort_add: list empty\n");
  499. list_add(&part->link, &dev->parts);
  500. dev->num_parts++;
  501. index_partitions();
  502. return 0;
  503. }
  504. new_pi = list_entry(&part->link, struct part_info, link);
  505. /* get current partition info if we are updating current device */
  506. curr_pi = NULL;
  507. if (dev == current_dev)
  508. curr_pi = mtd_part_info(current_dev, current_partnum);
  509. list_for_each(entry, &dev->parts) {
  510. struct part_info *pi;
  511. pi = list_entry(entry, struct part_info, link);
  512. /* be compliant with kernel cmdline, allow only one partition at offset zero */
  513. if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
  514. printf("cannot add second partition at offset 0\n");
  515. return 1;
  516. }
  517. if (new_pi->offset <= pi->offset) {
  518. list_add_tail(&part->link, entry);
  519. dev->num_parts++;
  520. if (curr_pi && (pi->offset <= curr_pi->offset)) {
  521. /* we are modyfing partitions for the current
  522. * device, update current */
  523. current_partnum++;
  524. current_save();
  525. } else {
  526. index_partitions();
  527. }
  528. return 0;
  529. }
  530. }
  531. list_add_tail(&part->link, &dev->parts);
  532. dev->num_parts++;
  533. index_partitions();
  534. return 0;
  535. }
  536. /**
  537. * Add provided partition to the partition list of a given device.
  538. *
  539. * @param dev device to which partition is added
  540. * @param part partition to be added
  541. * @return 0 on success, 1 otherwise
  542. */
  543. static int part_add(struct mtd_device *dev, struct part_info *part)
  544. {
  545. /* verify alignment and size */
  546. if (part_validate(dev->id, part) != 0)
  547. return 1;
  548. /* partition is ok, add it to the list */
  549. if (part_sort_add(dev, part) != 0)
  550. return 1;
  551. return 0;
  552. }
  553. /**
  554. * Parse one partition definition, allocate memory and return pointer to this
  555. * location in retpart.
  556. *
  557. * @param partdef pointer to the partition definition string i.e. <part-def>
  558. * @param ret output pointer to next char after parse completes (output)
  559. * @param retpart pointer to the allocated partition (output)
  560. * @return 0 on success, 1 otherwise
  561. */
  562. static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
  563. {
  564. struct part_info *part;
  565. unsigned long size;
  566. unsigned long offset;
  567. const char *name;
  568. int name_len;
  569. unsigned int mask_flags;
  570. const char *p;
  571. p = partdef;
  572. *retpart = NULL;
  573. *ret = NULL;
  574. /* fetch the partition size */
  575. if (*p == '-') {
  576. /* assign all remaining space to this partition */
  577. DEBUGF("'-': remaining size assigned\n");
  578. size = SIZE_REMAINING;
  579. p++;
  580. } else {
  581. size = memsize_parse(p, &p);
  582. if (size < MIN_PART_SIZE) {
  583. printf("partition size too small (%lx)\n", size);
  584. return 1;
  585. }
  586. }
  587. /* check for offset */
  588. offset = OFFSET_NOT_SPECIFIED;
  589. if (*p == '@') {
  590. p++;
  591. offset = memsize_parse(p, &p);
  592. }
  593. /* now look for the name */
  594. if (*p == '(') {
  595. name = ++p;
  596. if ((p = strchr(name, ')')) == NULL) {
  597. printf("no closing ) found in partition name\n");
  598. return 1;
  599. }
  600. name_len = p - name + 1;
  601. if ((name_len - 1) == 0) {
  602. printf("empty partition name\n");
  603. return 1;
  604. }
  605. p++;
  606. } else {
  607. /* 0x00000000@0x00000000 */
  608. name_len = 22;
  609. name = NULL;
  610. }
  611. /* test for options */
  612. mask_flags = 0;
  613. if (strncmp(p, "ro", 2) == 0) {
  614. mask_flags |= MTD_WRITEABLE_CMD;
  615. p += 2;
  616. }
  617. /* check for next partition definition */
  618. if (*p == ',') {
  619. if (size == SIZE_REMAINING) {
  620. *ret = NULL;
  621. printf("no partitions allowed after a fill-up partition\n");
  622. return 1;
  623. }
  624. *ret = ++p;
  625. } else if ((*p == ';') || (*p == '\0')) {
  626. *ret = p;
  627. } else {
  628. printf("unexpected character '%c' at the end of partition\n", *p);
  629. *ret = NULL;
  630. return 1;
  631. }
  632. /* allocate memory */
  633. part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
  634. if (!part) {
  635. printf("out of memory\n");
  636. return 1;
  637. }
  638. memset(part, 0, sizeof(struct part_info) + name_len);
  639. part->size = size;
  640. part->offset = offset;
  641. part->mask_flags = mask_flags;
  642. part->name = (char *)(part + 1);
  643. if (name) {
  644. /* copy user provided name */
  645. strncpy(part->name, name, name_len - 1);
  646. part->auto_name = 0;
  647. } else {
  648. /* auto generated name in form of size@offset */
  649. sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
  650. part->auto_name = 1;
  651. }
  652. part->name[name_len - 1] = '\0';
  653. INIT_LIST_HEAD(&part->link);
  654. DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
  655. part->name, part->size,
  656. part->offset, part->mask_flags);
  657. *retpart = part;
  658. return 0;
  659. }
  660. /**
  661. * Check device number to be within valid range for given device type.
  662. *
  663. * @param dev device to validate
  664. * @return 0 if device is valid, 1 otherwise
  665. */
  666. int mtd_device_validate(u8 type, u8 num, u32 *size)
  667. {
  668. if (type == MTD_DEV_TYPE_NOR) {
  669. #if defined(CONFIG_CMD_FLASH)
  670. if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
  671. extern flash_info_t flash_info[];
  672. *size = flash_info[num].size;
  673. return 0;
  674. }
  675. printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
  676. MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
  677. #else
  678. printf("support for FLASH devices not present\n");
  679. #endif
  680. } else if (type == MTD_DEV_TYPE_NAND) {
  681. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  682. if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
  683. #ifndef CONFIG_NAND_LEGACY
  684. *size = nand_info[num].size;
  685. #else
  686. extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
  687. *size = nand_dev_desc[num].totlen;
  688. #endif
  689. return 0;
  690. }
  691. printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
  692. MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
  693. #else
  694. printf("support for NAND devices not present\n");
  695. #endif
  696. } else if (type == MTD_DEV_TYPE_ONENAND) {
  697. #if defined(CONFIG_CMD_ONENAND)
  698. *size = onenand_mtd.size;
  699. return 0;
  700. #else
  701. printf("support for OneNAND devices not present\n");
  702. #endif
  703. } else
  704. printf("Unknown defice type %d\n", type);
  705. return 1;
  706. }
  707. /**
  708. * Delete all mtd devices from a supplied devices list, free memory allocated for
  709. * each device and delete all device partitions.
  710. *
  711. * @return 0 on success, 1 otherwise
  712. */
  713. static int device_delall(struct list_head *head)
  714. {
  715. struct list_head *entry, *n;
  716. struct mtd_device *dev_tmp;
  717. /* clean devices list */
  718. list_for_each_safe(entry, n, head) {
  719. dev_tmp = list_entry(entry, struct mtd_device, link);
  720. list_del(entry);
  721. part_delall(&dev_tmp->parts);
  722. free(dev_tmp);
  723. }
  724. INIT_LIST_HEAD(&devices);
  725. return 0;
  726. }
  727. /**
  728. * If provided device exists it's partitions are deleted, device is removed
  729. * from device list and device memory is freed.
  730. *
  731. * @param dev device to be deleted
  732. * @return 0 on success, 1 otherwise
  733. */
  734. static int device_del(struct mtd_device *dev)
  735. {
  736. part_delall(&dev->parts);
  737. list_del(&dev->link);
  738. free(dev);
  739. if (dev == current_dev) {
  740. /* we just deleted current device */
  741. if (list_empty(&devices)) {
  742. current_dev = NULL;
  743. } else {
  744. /* reset first partition from first dev from the
  745. * devices list as current */
  746. current_dev = list_entry(devices.next, struct mtd_device, link);
  747. current_partnum = 0;
  748. }
  749. current_save();
  750. return 0;
  751. }
  752. index_partitions();
  753. return 0;
  754. }
  755. /**
  756. * Search global device list and return pointer to the device of type and num
  757. * specified.
  758. *
  759. * @param type device type
  760. * @param num device number
  761. * @return NULL if requested device does not exist
  762. */
  763. static struct mtd_device* device_find(u8 type, u8 num)
  764. {
  765. struct list_head *entry;
  766. struct mtd_device *dev_tmp;
  767. list_for_each(entry, &devices) {
  768. dev_tmp = list_entry(entry, struct mtd_device, link);
  769. if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
  770. return dev_tmp;
  771. }
  772. return NULL;
  773. }
  774. /**
  775. * Add specified device to the global device list.
  776. *
  777. * @param dev device to be added
  778. */
  779. static void device_add(struct mtd_device *dev)
  780. {
  781. u8 current_save_needed = 0;
  782. if (list_empty(&devices)) {
  783. current_dev = dev;
  784. current_partnum = 0;
  785. current_save_needed = 1;
  786. }
  787. list_add_tail(&dev->link, &devices);
  788. if (current_save_needed > 0)
  789. current_save();
  790. else
  791. index_partitions();
  792. }
  793. /**
  794. * Parse device type, name and mtd-id. If syntax is ok allocate memory and
  795. * return pointer to the device structure.
  796. *
  797. * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
  798. * @param ret output pointer to next char after parse completes (output)
  799. * @param retdev pointer to the allocated device (output)
  800. * @return 0 on success, 1 otherwise
  801. */
  802. static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
  803. {
  804. struct mtd_device *dev;
  805. struct part_info *part;
  806. struct mtdids *id;
  807. const char *mtd_id;
  808. unsigned int mtd_id_len;
  809. const char *p, *pend;
  810. LIST_HEAD(tmp_list);
  811. struct list_head *entry, *n;
  812. u16 num_parts;
  813. u32 offset;
  814. int err = 1;
  815. p = mtd_dev;
  816. *retdev = NULL;
  817. *ret = NULL;
  818. DEBUGF("===device_parse===\n");
  819. /* fetch <mtd-id> */
  820. mtd_id = p;
  821. if (!(p = strchr(mtd_id, ':'))) {
  822. printf("no <mtd-id> identifier\n");
  823. return 1;
  824. }
  825. mtd_id_len = p - mtd_id + 1;
  826. p++;
  827. /* verify if we have a valid device specified */
  828. if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
  829. printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
  830. return 1;
  831. }
  832. DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
  833. id->type, MTD_DEV_TYPE(id->type),
  834. id->num, id->mtd_id);
  835. pend = strchr(p, ';');
  836. DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
  837. /* parse partitions */
  838. num_parts = 0;
  839. offset = 0;
  840. if ((dev = device_find(id->type, id->num)) != NULL) {
  841. /* if device already exists start at the end of the last partition */
  842. part = list_entry(dev->parts.prev, struct part_info, link);
  843. offset = part->offset + part->size;
  844. }
  845. while (p && (*p != '\0') && (*p != ';')) {
  846. err = 1;
  847. if ((part_parse(p, &p, &part) != 0) || (!part))
  848. break;
  849. /* calculate offset when not specified */
  850. if (part->offset == OFFSET_NOT_SPECIFIED)
  851. part->offset = offset;
  852. else
  853. offset = part->offset;
  854. /* verify alignment and size */
  855. if (part_validate(id, part) != 0)
  856. break;
  857. offset += part->size;
  858. /* partition is ok, add it to the list */
  859. list_add_tail(&part->link, &tmp_list);
  860. num_parts++;
  861. err = 0;
  862. }
  863. if (err == 1) {
  864. part_delall(&tmp_list);
  865. return 1;
  866. }
  867. if (num_parts == 0) {
  868. printf("no partitions for device %s%d (%s)\n",
  869. MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
  870. return 1;
  871. }
  872. DEBUGF("\ntotal partitions: %d\n", num_parts);
  873. /* check for next device presence */
  874. if (p) {
  875. if (*p == ';') {
  876. *ret = ++p;
  877. } else if (*p == '\0') {
  878. *ret = p;
  879. } else {
  880. printf("unexpected character '%c' at the end of device\n", *p);
  881. *ret = NULL;
  882. return 1;
  883. }
  884. }
  885. /* allocate memory for mtd_device structure */
  886. if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
  887. printf("out of memory\n");
  888. return 1;
  889. }
  890. memset(dev, 0, sizeof(struct mtd_device));
  891. dev->id = id;
  892. dev->num_parts = 0; /* part_sort_add increments num_parts */
  893. INIT_LIST_HEAD(&dev->parts);
  894. INIT_LIST_HEAD(&dev->link);
  895. /* move partitions from tmp_list to dev->parts */
  896. list_for_each_safe(entry, n, &tmp_list) {
  897. part = list_entry(entry, struct part_info, link);
  898. list_del(entry);
  899. if (part_sort_add(dev, part) != 0) {
  900. device_del(dev);
  901. return 1;
  902. }
  903. }
  904. *retdev = dev;
  905. DEBUGF("===\n\n");
  906. return 0;
  907. }
  908. /**
  909. * Initialize global device list.
  910. *
  911. * @return 0 on success, 1 otherwise
  912. */
  913. static int mtd_devices_init(void)
  914. {
  915. last_parts[0] = '\0';
  916. current_dev = NULL;
  917. current_save();
  918. return device_delall(&devices);
  919. }
  920. /*
  921. * Search global mtdids list and find id of requested type and number.
  922. *
  923. * @return pointer to the id if it exists, NULL otherwise
  924. */
  925. static struct mtdids* id_find(u8 type, u8 num)
  926. {
  927. struct list_head *entry;
  928. struct mtdids *id;
  929. list_for_each(entry, &mtdids) {
  930. id = list_entry(entry, struct mtdids, link);
  931. if ((id->type == type) && (id->num == num))
  932. return id;
  933. }
  934. return NULL;
  935. }
  936. /**
  937. * Search global mtdids list and find id of a requested mtd_id.
  938. *
  939. * Note: first argument is not null terminated.
  940. *
  941. * @param mtd_id string containing requested mtd_id
  942. * @param mtd_id_len length of supplied mtd_id
  943. * @return pointer to the id if it exists, NULL otherwise
  944. */
  945. static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
  946. {
  947. struct list_head *entry;
  948. struct mtdids *id;
  949. DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
  950. mtd_id_len, mtd_id, mtd_id_len);
  951. list_for_each(entry, &mtdids) {
  952. id = list_entry(entry, struct mtdids, link);
  953. DEBUGF("entry: '%s' (len = %d)\n",
  954. id->mtd_id, strlen(id->mtd_id));
  955. if (mtd_id_len != strlen(id->mtd_id))
  956. continue;
  957. if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
  958. return id;
  959. }
  960. return NULL;
  961. }
  962. /**
  963. * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
  964. * return device type and number.
  965. *
  966. * @param id string describing device id
  967. * @param ret_id output pointer to next char after parse completes (output)
  968. * @param dev_type parsed device type (output)
  969. * @param dev_num parsed device number (output)
  970. * @return 0 on success, 1 otherwise
  971. */
  972. int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
  973. {
  974. const char *p = id;
  975. *dev_type = 0;
  976. if (strncmp(p, "nand", 4) == 0) {
  977. *dev_type = MTD_DEV_TYPE_NAND;
  978. p += 4;
  979. } else if (strncmp(p, "nor", 3) == 0) {
  980. *dev_type = MTD_DEV_TYPE_NOR;
  981. p += 3;
  982. } else if (strncmp(p, "onenand", 7) == 0) {
  983. *dev_type = MTD_DEV_TYPE_ONENAND;
  984. p += 7;
  985. } else {
  986. printf("incorrect device type in %s\n", id);
  987. return 1;
  988. }
  989. if (!isdigit(*p)) {
  990. printf("incorrect device number in %s\n", id);
  991. return 1;
  992. }
  993. *dev_num = simple_strtoul(p, (char **)&p, 0);
  994. if (ret_id)
  995. *ret_id = p;
  996. return 0;
  997. }
  998. /**
  999. * Process all devices and generate corresponding mtdparts string describing
  1000. * all partitions on all devices.
  1001. *
  1002. * @param buf output buffer holding generated mtdparts string (output)
  1003. * @param buflen buffer size
  1004. * @return 0 on success, 1 otherwise
  1005. */
  1006. static int generate_mtdparts(char *buf, u32 buflen)
  1007. {
  1008. struct list_head *pentry, *dentry;
  1009. struct mtd_device *dev;
  1010. struct part_info *part, *prev_part;
  1011. char *p = buf;
  1012. char tmpbuf[32];
  1013. u32 size, offset, len, part_cnt;
  1014. u32 maxlen = buflen - 1;
  1015. DEBUGF("--- generate_mtdparts ---\n");
  1016. if (list_empty(&devices)) {
  1017. buf[0] = '\0';
  1018. return 0;
  1019. }
  1020. sprintf(p, "mtdparts=");
  1021. p += 9;
  1022. list_for_each(dentry, &devices) {
  1023. dev = list_entry(dentry, struct mtd_device, link);
  1024. /* copy mtd_id */
  1025. len = strlen(dev->id->mtd_id) + 1;
  1026. if (len > maxlen)
  1027. goto cleanup;
  1028. memcpy(p, dev->id->mtd_id, len - 1);
  1029. p += len - 1;
  1030. *(p++) = ':';
  1031. maxlen -= len;
  1032. /* format partitions */
  1033. prev_part = NULL;
  1034. part_cnt = 0;
  1035. list_for_each(pentry, &dev->parts) {
  1036. part = list_entry(pentry, struct part_info, link);
  1037. size = part->size;
  1038. offset = part->offset;
  1039. part_cnt++;
  1040. /* partition size */
  1041. memsize_format(tmpbuf, size);
  1042. len = strlen(tmpbuf);
  1043. if (len > maxlen)
  1044. goto cleanup;
  1045. memcpy(p, tmpbuf, len);
  1046. p += len;
  1047. maxlen -= len;
  1048. /* add offset only when there is a gap between
  1049. * partitions */
  1050. if ((!prev_part && (offset != 0)) ||
  1051. (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
  1052. memsize_format(tmpbuf, offset);
  1053. len = strlen(tmpbuf) + 1;
  1054. if (len > maxlen)
  1055. goto cleanup;
  1056. *(p++) = '@';
  1057. memcpy(p, tmpbuf, len - 1);
  1058. p += len - 1;
  1059. maxlen -= len;
  1060. }
  1061. /* copy name only if user supplied */
  1062. if(!part->auto_name) {
  1063. len = strlen(part->name) + 2;
  1064. if (len > maxlen)
  1065. goto cleanup;
  1066. *(p++) = '(';
  1067. memcpy(p, part->name, len - 2);
  1068. p += len - 2;
  1069. *(p++) = ')';
  1070. maxlen -= len;
  1071. }
  1072. /* ro mask flag */
  1073. if (part->mask_flags && MTD_WRITEABLE_CMD) {
  1074. len = 2;
  1075. if (len > maxlen)
  1076. goto cleanup;
  1077. *(p++) = 'r';
  1078. *(p++) = 'o';
  1079. maxlen -= 2;
  1080. }
  1081. /* print ',' separator if there are other partitions
  1082. * following */
  1083. if (dev->num_parts > part_cnt) {
  1084. if (1 > maxlen)
  1085. goto cleanup;
  1086. *(p++) = ',';
  1087. maxlen--;
  1088. }
  1089. prev_part = part;
  1090. }
  1091. /* print ';' separator if there are other devices following */
  1092. if (dentry->next != &devices) {
  1093. if (1 > maxlen)
  1094. goto cleanup;
  1095. *(p++) = ';';
  1096. maxlen--;
  1097. }
  1098. }
  1099. /* we still have at least one char left, as we decremented maxlen at
  1100. * the begining */
  1101. *p = '\0';
  1102. return 0;
  1103. cleanup:
  1104. last_parts[0] = '\0';
  1105. return 1;
  1106. }
  1107. /**
  1108. * Call generate_mtdparts to process all devices and generate corresponding
  1109. * mtdparts string, save it in mtdparts environment variable.
  1110. *
  1111. * @param buf output buffer holding generated mtdparts string (output)
  1112. * @param buflen buffer size
  1113. * @return 0 on success, 1 otherwise
  1114. */
  1115. static int generate_mtdparts_save(char *buf, u32 buflen)
  1116. {
  1117. int ret;
  1118. ret = generate_mtdparts(buf, buflen);
  1119. if ((buf[0] != '\0') && (ret == 0))
  1120. setenv("mtdparts", buf);
  1121. else
  1122. setenv("mtdparts", NULL);
  1123. return ret;
  1124. }
  1125. /**
  1126. * Format and print out a partition list for each device from global device
  1127. * list.
  1128. */
  1129. static void list_partitions(void)
  1130. {
  1131. struct list_head *dentry, *pentry;
  1132. struct part_info *part;
  1133. struct mtd_device *dev;
  1134. int part_num;
  1135. DEBUGF("\n---list_partitions---\n");
  1136. list_for_each(dentry, &devices) {
  1137. dev = list_entry(dentry, struct mtd_device, link);
  1138. printf("\ndevice %s%d <%s>, # parts = %d\n",
  1139. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1140. dev->id->mtd_id, dev->num_parts);
  1141. printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
  1142. /* list partitions for given device */
  1143. part_num = 0;
  1144. list_for_each(pentry, &dev->parts) {
  1145. part = list_entry(pentry, struct part_info, link);
  1146. printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
  1147. part_num, part->name, part->size,
  1148. part->offset, part->mask_flags);
  1149. part_num++;
  1150. }
  1151. }
  1152. if (list_empty(&devices))
  1153. printf("no partitions defined\n");
  1154. /* current_dev is not NULL only when we have non empty device list */
  1155. if (current_dev) {
  1156. part = mtd_part_info(current_dev, current_partnum);
  1157. if (part) {
  1158. printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
  1159. MTD_DEV_TYPE(current_dev->id->type),
  1160. current_dev->id->num, current_partnum,
  1161. part->name, part->size, part->offset);
  1162. } else {
  1163. printf("could not get current partition info\n\n");
  1164. }
  1165. }
  1166. printf("\ndefaults:\n");
  1167. printf("mtdids : %s\n", mtdids_default);
  1168. printf("mtdparts: %s\n", mtdparts_default);
  1169. }
  1170. /**
  1171. * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
  1172. * corresponding device and verify partition number.
  1173. *
  1174. * @param id string describing device and partition or partition name
  1175. * @param dev pointer to the requested device (output)
  1176. * @param part_num verified partition number (output)
  1177. * @param part pointer to requested partition (output)
  1178. * @return 0 on success, 1 otherwise
  1179. */
  1180. int find_dev_and_part(const char *id, struct mtd_device **dev,
  1181. u8 *part_num, struct part_info **part)
  1182. {
  1183. struct list_head *dentry, *pentry;
  1184. u8 type, dnum, pnum;
  1185. const char *p;
  1186. DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
  1187. list_for_each(dentry, &devices) {
  1188. *part_num = 0;
  1189. *dev = list_entry(dentry, struct mtd_device, link);
  1190. list_for_each(pentry, &(*dev)->parts) {
  1191. *part = list_entry(pentry, struct part_info, link);
  1192. if (strcmp((*part)->name, id) == 0)
  1193. return 0;
  1194. (*part_num)++;
  1195. }
  1196. }
  1197. p = id;
  1198. *dev = NULL;
  1199. *part = NULL;
  1200. *part_num = 0;
  1201. if (mtd_id_parse(p, &p, &type, &dnum) != 0)
  1202. return 1;
  1203. if ((*p++ != ',') || (*p == '\0')) {
  1204. printf("no partition number specified\n");
  1205. return 1;
  1206. }
  1207. pnum = simple_strtoul(p, (char **)&p, 0);
  1208. if (*p != '\0') {
  1209. printf("unexpected trailing character '%c'\n", *p);
  1210. return 1;
  1211. }
  1212. if ((*dev = device_find(type, dnum)) == NULL) {
  1213. printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
  1214. return 1;
  1215. }
  1216. if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
  1217. printf("no such partition\n");
  1218. *dev = NULL;
  1219. return 1;
  1220. }
  1221. *part_num = pnum;
  1222. return 0;
  1223. }
  1224. /**
  1225. * Find and delete partition. For partition id format see find_dev_and_part().
  1226. *
  1227. * @param id string describing device and partition
  1228. * @return 0 on success, 1 otherwise
  1229. */
  1230. static int delete_partition(const char *id)
  1231. {
  1232. u8 pnum;
  1233. struct mtd_device *dev;
  1234. struct part_info *part;
  1235. if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
  1236. DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
  1237. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
  1238. part->name, part->size, part->offset);
  1239. if (part_del(dev, part) != 0)
  1240. return 1;
  1241. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1242. printf("generated mtdparts too long, reseting to null\n");
  1243. return 1;
  1244. }
  1245. return 0;
  1246. }
  1247. printf("partition %s not found\n", id);
  1248. return 1;
  1249. }
  1250. /**
  1251. * Accept character string describing mtd partitions and call device_parse()
  1252. * for each entry. Add created devices to the global devices list.
  1253. *
  1254. * @param mtdparts string specifing mtd partitions
  1255. * @return 0 on success, 1 otherwise
  1256. */
  1257. static int parse_mtdparts(const char *const mtdparts)
  1258. {
  1259. const char *p = mtdparts;
  1260. struct mtd_device *dev;
  1261. int err = 1;
  1262. DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
  1263. /* delete all devices and partitions */
  1264. if (mtd_devices_init() != 0) {
  1265. printf("could not initialise device list\n");
  1266. return err;
  1267. }
  1268. /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
  1269. p = getenv("mtdparts");
  1270. if (strncmp(p, "mtdparts=", 9) != 0) {
  1271. printf("mtdparts variable doesn't start with 'mtdparts='\n");
  1272. return err;
  1273. }
  1274. p += 9;
  1275. while (p && (*p != '\0')) {
  1276. err = 1;
  1277. if ((device_parse(p, &p, &dev) != 0) || (!dev))
  1278. break;
  1279. DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1280. dev->id->num, dev->id->mtd_id);
  1281. /* check if parsed device is already on the list */
  1282. if (device_find(dev->id->type, dev->id->num) != NULL) {
  1283. printf("device %s%d redefined, please correct mtdparts variable\n",
  1284. MTD_DEV_TYPE(dev->id->type), dev->id->num);
  1285. break;
  1286. }
  1287. list_add_tail(&dev->link, &devices);
  1288. err = 0;
  1289. }
  1290. if (err == 1) {
  1291. device_delall(&devices);
  1292. return 1;
  1293. }
  1294. return 0;
  1295. }
  1296. /**
  1297. * Parse provided string describing mtdids mapping (see file header for mtdids
  1298. * variable format). Allocate memory for each entry and add all found entries
  1299. * to the global mtdids list.
  1300. *
  1301. * @param ids mapping string
  1302. * @return 0 on success, 1 otherwise
  1303. */
  1304. static int parse_mtdids(const char *const ids)
  1305. {
  1306. const char *p = ids;
  1307. const char *mtd_id;
  1308. int mtd_id_len;
  1309. struct mtdids *id;
  1310. struct list_head *entry, *n;
  1311. struct mtdids *id_tmp;
  1312. u8 type, num;
  1313. u32 size;
  1314. int ret = 1;
  1315. DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
  1316. /* clean global mtdids list */
  1317. list_for_each_safe(entry, n, &mtdids) {
  1318. id_tmp = list_entry(entry, struct mtdids, link);
  1319. DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
  1320. list_del(entry);
  1321. free(id_tmp);
  1322. }
  1323. last_ids[0] = '\0';
  1324. INIT_LIST_HEAD(&mtdids);
  1325. while(p && (*p != '\0')) {
  1326. ret = 1;
  1327. /* parse 'nor'|'nand'|'onenand'<dev-num> */
  1328. if (mtd_id_parse(p, &p, &type, &num) != 0)
  1329. break;
  1330. if (*p != '=') {
  1331. printf("mtdids: incorrect <dev-num>\n");
  1332. break;
  1333. }
  1334. p++;
  1335. /* check if requested device exists */
  1336. if (mtd_device_validate(type, num, &size) != 0)
  1337. return 1;
  1338. /* locate <mtd-id> */
  1339. mtd_id = p;
  1340. if ((p = strchr(mtd_id, ',')) != NULL) {
  1341. mtd_id_len = p - mtd_id + 1;
  1342. p++;
  1343. } else {
  1344. mtd_id_len = strlen(mtd_id) + 1;
  1345. }
  1346. if (mtd_id_len == 0) {
  1347. printf("mtdids: no <mtd-id> identifier\n");
  1348. break;
  1349. }
  1350. /* check if this id is already on the list */
  1351. int double_entry = 0;
  1352. list_for_each(entry, &mtdids) {
  1353. id_tmp = list_entry(entry, struct mtdids, link);
  1354. if ((id_tmp->type == type) && (id_tmp->num == num)) {
  1355. double_entry = 1;
  1356. break;
  1357. }
  1358. }
  1359. if (double_entry) {
  1360. printf("device id %s%d redefined, please correct mtdids variable\n",
  1361. MTD_DEV_TYPE(type), num);
  1362. break;
  1363. }
  1364. /* allocate mtdids structure */
  1365. if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
  1366. printf("out of memory\n");
  1367. break;
  1368. }
  1369. memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
  1370. id->num = num;
  1371. id->type = type;
  1372. id->size = size;
  1373. id->mtd_id = (char *)(id + 1);
  1374. strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
  1375. id->mtd_id[mtd_id_len - 1] = '\0';
  1376. INIT_LIST_HEAD(&id->link);
  1377. DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
  1378. MTD_DEV_TYPE(id->type), id->num,
  1379. id->size, id->mtd_id);
  1380. list_add_tail(&id->link, &mtdids);
  1381. ret = 0;
  1382. }
  1383. if (ret == 1) {
  1384. /* clean mtdids list and free allocated memory */
  1385. list_for_each_safe(entry, n, &mtdids) {
  1386. id_tmp = list_entry(entry, struct mtdids, link);
  1387. list_del(entry);
  1388. free(id_tmp);
  1389. }
  1390. return 1;
  1391. }
  1392. return 0;
  1393. }
  1394. /**
  1395. * Parse and initialize global mtdids mapping and create global
  1396. * device/partition list.
  1397. *
  1398. * @return 0 on success, 1 otherwise
  1399. */
  1400. int mtdparts_init(void)
  1401. {
  1402. static int initialized = 0;
  1403. const char *ids, *parts;
  1404. const char *current_partition;
  1405. int ids_changed;
  1406. char tmp_ep[PARTITION_MAXLEN];
  1407. DEBUGF("\n---mtdparts_init---\n");
  1408. if (!initialized) {
  1409. INIT_LIST_HEAD(&mtdids);
  1410. INIT_LIST_HEAD(&devices);
  1411. memset(last_ids, 0, MTDIDS_MAXLEN);
  1412. memset(last_parts, 0, MTDPARTS_MAXLEN);
  1413. memset(last_partition, 0, PARTITION_MAXLEN);
  1414. initialized = 1;
  1415. }
  1416. /* get variables */
  1417. ids = getenv("mtdids");
  1418. parts = getenv("mtdparts");
  1419. current_partition = getenv("partition");
  1420. /* save it for later parsing, cannot rely on current partition pointer
  1421. * as 'partition' variable may be updated during init */
  1422. tmp_ep[0] = '\0';
  1423. if (current_partition)
  1424. strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
  1425. DEBUGF("last_ids : %s\n", last_ids);
  1426. DEBUGF("env_ids : %s\n", ids);
  1427. DEBUGF("last_parts: %s\n", last_parts);
  1428. DEBUGF("env_parts : %s\n\n", parts);
  1429. DEBUGF("last_partition : %s\n", last_partition);
  1430. DEBUGF("env_partition : %s\n", current_partition);
  1431. /* if mtdids varible is empty try to use defaults */
  1432. if (!ids) {
  1433. if (mtdids_default) {
  1434. DEBUGF("mtdids variable not defined, using default\n");
  1435. ids = mtdids_default;
  1436. setenv("mtdids", (char *)ids);
  1437. } else {
  1438. printf("mtdids not defined, no default present\n");
  1439. return 1;
  1440. }
  1441. }
  1442. if (strlen(ids) > MTDIDS_MAXLEN - 1) {
  1443. printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
  1444. return 1;
  1445. }
  1446. /* do no try to use defaults when mtdparts variable is not defined,
  1447. * just check the length */
  1448. if (!parts)
  1449. printf("mtdparts variable not set, see 'help mtdparts'\n");
  1450. if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
  1451. printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
  1452. return 1;
  1453. }
  1454. /* check if we have already parsed those mtdids */
  1455. if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
  1456. ids_changed = 0;
  1457. } else {
  1458. ids_changed = 1;
  1459. if (parse_mtdids(ids) != 0) {
  1460. mtd_devices_init();
  1461. return 1;
  1462. }
  1463. /* ok it's good, save new ids */
  1464. strncpy(last_ids, ids, MTDIDS_MAXLEN);
  1465. }
  1466. /* parse partitions if either mtdparts or mtdids were updated */
  1467. if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
  1468. if (parse_mtdparts(parts) != 0)
  1469. return 1;
  1470. if (list_empty(&devices)) {
  1471. printf("mtdparts_init: no valid partitions\n");
  1472. return 1;
  1473. }
  1474. /* ok it's good, save new parts */
  1475. strncpy(last_parts, parts, MTDPARTS_MAXLEN);
  1476. /* reset first partition from first dev from the list as current */
  1477. current_dev = list_entry(devices.next, struct mtd_device, link);
  1478. current_partnum = 0;
  1479. current_save();
  1480. DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
  1481. MTD_DEV_TYPE(current_dev->id->type),
  1482. current_dev->id->num, current_partnum);
  1483. }
  1484. /* mtdparts variable was reset to NULL, delete all devices/partitions */
  1485. if (!parts && (last_parts[0] != '\0'))
  1486. return mtd_devices_init();
  1487. /* do not process current partition if mtdparts variable is null */
  1488. if (!parts)
  1489. return 0;
  1490. /* is current partition set in environment? if so, use it */
  1491. if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
  1492. struct part_info *p;
  1493. struct mtd_device *cdev;
  1494. u8 pnum;
  1495. DEBUGF("--- getting current partition: %s\n", tmp_ep);
  1496. if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
  1497. current_dev = cdev;
  1498. current_partnum = pnum;
  1499. current_save();
  1500. }
  1501. } else if (getenv("partition") == NULL) {
  1502. DEBUGF("no partition variable set, setting...\n");
  1503. current_save();
  1504. }
  1505. return 0;
  1506. }
  1507. /**
  1508. * Return pointer to the partition of a requested number from a requested
  1509. * device.
  1510. *
  1511. * @param dev device that is to be searched for a partition
  1512. * @param part_num requested partition number
  1513. * @return pointer to the part_info, NULL otherwise
  1514. */
  1515. static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
  1516. {
  1517. struct list_head *entry;
  1518. struct part_info *part;
  1519. int num;
  1520. if (!dev)
  1521. return NULL;
  1522. DEBUGF("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
  1523. part_num, MTD_DEV_TYPE(dev->id->type),
  1524. dev->id->num, dev->id->mtd_id);
  1525. if (part_num >= dev->num_parts) {
  1526. printf("invalid partition number %d for device %s%d (%s)\n",
  1527. part_num, MTD_DEV_TYPE(dev->id->type),
  1528. dev->id->num, dev->id->mtd_id);
  1529. return NULL;
  1530. }
  1531. /* locate partition number, return it */
  1532. num = 0;
  1533. list_for_each(entry, &dev->parts) {
  1534. part = list_entry(entry, struct part_info, link);
  1535. if (part_num == num++) {
  1536. return part;
  1537. }
  1538. }
  1539. return NULL;
  1540. }
  1541. /***************************************************/
  1542. /* U-boot commands */
  1543. /***************************************************/
  1544. /* command line only */
  1545. /**
  1546. * Routine implementing u-boot chpart command. Sets new current partition based
  1547. * on the user supplied partition id. For partition id format see find_dev_and_part().
  1548. *
  1549. * @param cmdtp command internal data
  1550. * @param flag command flag
  1551. * @param argc number of arguments supplied to the command
  1552. * @param argv arguments list
  1553. * @return 0 on success, 1 otherwise
  1554. */
  1555. int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1556. {
  1557. /* command line only */
  1558. struct mtd_device *dev;
  1559. struct part_info *part;
  1560. u8 pnum;
  1561. if (mtdparts_init() !=0)
  1562. return 1;
  1563. if (argc < 2) {
  1564. printf("no partition id specified\n");
  1565. return 1;
  1566. }
  1567. if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
  1568. return 1;
  1569. current_dev = dev;
  1570. current_partnum = pnum;
  1571. current_save();
  1572. printf("partition changed to %s%d,%d\n",
  1573. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
  1574. return 0;
  1575. }
  1576. /**
  1577. * Routine implementing u-boot mtdparts command. Initialize/update default global
  1578. * partition list and process user partition request (list, add, del).
  1579. *
  1580. * @param cmdtp command internal data
  1581. * @param flag command flag
  1582. * @param argc number of arguments supplied to the command
  1583. * @param argv arguments list
  1584. * @return 0 on success, 1 otherwise
  1585. */
  1586. int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1587. {
  1588. if (argc == 2) {
  1589. if (strcmp(argv[1], "default") == 0) {
  1590. setenv("mtdids", (char *)mtdids_default);
  1591. setenv("mtdparts", (char *)mtdparts_default);
  1592. setenv("partition", NULL);
  1593. mtdparts_init();
  1594. return 0;
  1595. } else if (strcmp(argv[1], "delall") == 0) {
  1596. /* this may be the first run, initialize lists if needed */
  1597. mtdparts_init();
  1598. setenv("mtdparts", NULL);
  1599. /* mtd_devices_init() calls current_save() */
  1600. return mtd_devices_init();
  1601. }
  1602. }
  1603. /* make sure we are in sync with env variables */
  1604. if (mtdparts_init() != 0)
  1605. return 1;
  1606. if (argc == 1) {
  1607. list_partitions();
  1608. return 0;
  1609. }
  1610. /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
  1611. if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
  1612. #define PART_ADD_DESC_MAXLEN 64
  1613. char tmpbuf[PART_ADD_DESC_MAXLEN];
  1614. u8 type, num, len;
  1615. struct mtd_device *dev;
  1616. struct mtd_device *dev_tmp;
  1617. struct mtdids *id;
  1618. struct part_info *p;
  1619. if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
  1620. return 1;
  1621. if ((id = id_find(type, num)) == NULL) {
  1622. printf("no such device %s defined in mtdids variable\n", argv[2]);
  1623. return 1;
  1624. }
  1625. len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
  1626. len += strlen(argv[3]); /* size@offset */
  1627. len += strlen(argv[4]) + 2; /* '(' name ')' */
  1628. if (argv[5] && (strlen(argv[5]) == 2))
  1629. len += 2; /* 'ro' */
  1630. if (len >= PART_ADD_DESC_MAXLEN) {
  1631. printf("too long partition description\n");
  1632. return 1;
  1633. }
  1634. sprintf(tmpbuf, "%s:%s(%s)%s",
  1635. id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
  1636. DEBUGF("add tmpbuf: %s\n", tmpbuf);
  1637. if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
  1638. return 1;
  1639. DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1640. dev->id->num, dev->id->mtd_id);
  1641. if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
  1642. device_add(dev);
  1643. } else {
  1644. /* merge new partition with existing ones*/
  1645. p = list_entry(dev->parts.next, struct part_info, link);
  1646. if (part_add(dev_tmp, p) != 0) {
  1647. device_del(dev);
  1648. return 1;
  1649. }
  1650. }
  1651. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1652. printf("generated mtdparts too long, reseting to null\n");
  1653. return 1;
  1654. }
  1655. return 0;
  1656. }
  1657. /* mtdparts del part-id */
  1658. if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
  1659. DEBUGF("del: part-id = %s\n", argv[2]);
  1660. return delete_partition(argv[2]);
  1661. }
  1662. cmd_usage(cmdtp);
  1663. return 1;
  1664. }
  1665. /***************************************************/
  1666. U_BOOT_CMD(
  1667. chpart, 2, 0, do_chpart,
  1668. "change active partition",
  1669. "part-id\n"
  1670. " - change active partition (e.g. part-id = nand0,1)\n"
  1671. );
  1672. U_BOOT_CMD(
  1673. mtdparts, 6, 0, do_mtdparts,
  1674. "define flash/nand partitions",
  1675. "\n"
  1676. " - list partition table\n"
  1677. "mtdparts delall\n"
  1678. " - delete all partitions\n"
  1679. "mtdparts del part-id\n"
  1680. " - delete partition (e.g. part-id = nand0,1)\n"
  1681. "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
  1682. " - add partition\n"
  1683. "mtdparts default\n"
  1684. " - reset partition table to defaults\n\n"
  1685. "-----\n\n"
  1686. "this command uses three environment variables:\n\n"
  1687. "'partition' - keeps current partition identifier\n\n"
  1688. "partition := <part-id>\n"
  1689. "<part-id> := <dev-id>,part_num\n\n"
  1690. "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
  1691. "mtdids=<idmap>[,<idmap>,...]\n\n"
  1692. "<idmap> := <dev-id>=<mtd-id>\n"
  1693. "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
  1694. "<dev-num> := mtd device number, 0...\n"
  1695. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
  1696. "'mtdparts' - partition list\n\n"
  1697. "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
  1698. "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
  1699. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
  1700. "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
  1701. "<size> := standard linux memsize OR '-' to denote all remaining space\n"
  1702. "<offset> := partition start offset within the device\n"
  1703. "<name> := '(' NAME ')'\n"
  1704. "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
  1705. );
  1706. /***************************************************/