cmd_mtdparts.c 47 KB

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