cmd_mtdparts.c 47 KB

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