cmd_mtdparts.c 49 KB

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