part.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <ide.h>
  10. #include <malloc.h>
  11. #include <part.h>
  12. #undef PART_DEBUG
  13. #ifdef PART_DEBUG
  14. #define PRINTF(fmt,args...) printf (fmt ,##args)
  15. #else
  16. #define PRINTF(fmt,args...)
  17. #endif
  18. struct block_drvr {
  19. char *name;
  20. block_dev_desc_t* (*get_dev)(int dev);
  21. int (*select_hwpart)(int dev_num, int hwpart);
  22. };
  23. static const struct block_drvr block_drvr[] = {
  24. #if defined(CONFIG_CMD_IDE)
  25. { .name = "ide", .get_dev = ide_get_dev, },
  26. #endif
  27. #if defined(CONFIG_CMD_SATA)
  28. {.name = "sata", .get_dev = sata_get_dev, },
  29. #endif
  30. #if defined(CONFIG_CMD_SCSI)
  31. { .name = "scsi", .get_dev = scsi_get_dev, },
  32. #endif
  33. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  34. { .name = "usb", .get_dev = usb_stor_get_dev, },
  35. #endif
  36. #if defined(CONFIG_MMC)
  37. {
  38. .name = "mmc",
  39. .get_dev = mmc_get_dev,
  40. .select_hwpart = mmc_select_hwpart,
  41. },
  42. #endif
  43. #if defined(CONFIG_SYSTEMACE)
  44. { .name = "ace", .get_dev = systemace_get_dev, },
  45. #endif
  46. #if defined(CONFIG_SANDBOX)
  47. { .name = "host", .get_dev = host_get_dev, },
  48. #endif
  49. { },
  50. };
  51. DECLARE_GLOBAL_DATA_PTR;
  52. #ifdef HAVE_BLOCK_DEVICE
  53. static block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  54. {
  55. const struct block_drvr *drvr = block_drvr;
  56. block_dev_desc_t* (*reloc_get_dev)(int dev);
  57. int (*select_hwpart)(int dev_num, int hwpart);
  58. char *name;
  59. int ret;
  60. if (!ifname)
  61. return NULL;
  62. name = drvr->name;
  63. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  64. name += gd->reloc_off;
  65. #endif
  66. while (drvr->name) {
  67. name = drvr->name;
  68. reloc_get_dev = drvr->get_dev;
  69. select_hwpart = drvr->select_hwpart;
  70. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  71. name += gd->reloc_off;
  72. reloc_get_dev += gd->reloc_off;
  73. if (select_hwpart)
  74. select_hwpart += gd->reloc_off;
  75. #endif
  76. if (strncmp(ifname, name, strlen(name)) == 0) {
  77. block_dev_desc_t *dev_desc = reloc_get_dev(dev);
  78. if (!dev_desc)
  79. return NULL;
  80. if (hwpart == 0 && !select_hwpart)
  81. return dev_desc;
  82. if (!select_hwpart)
  83. return NULL;
  84. ret = select_hwpart(dev_desc->dev, hwpart);
  85. if (ret < 0)
  86. return NULL;
  87. return dev_desc;
  88. }
  89. drvr++;
  90. }
  91. return NULL;
  92. }
  93. block_dev_desc_t *get_dev(const char *ifname, int dev)
  94. {
  95. return get_dev_hwpart(ifname, dev, 0);
  96. }
  97. #else
  98. block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  99. {
  100. return NULL;
  101. }
  102. block_dev_desc_t *get_dev(const char *ifname, int dev)
  103. {
  104. return NULL;
  105. }
  106. #endif
  107. #ifdef HAVE_BLOCK_DEVICE
  108. /* ------------------------------------------------------------------------- */
  109. /*
  110. * reports device info to the user
  111. */
  112. #ifdef CONFIG_LBA48
  113. typedef uint64_t lba512_t;
  114. #else
  115. typedef lbaint_t lba512_t;
  116. #endif
  117. /*
  118. * Overflowless variant of (block_count * mul_by / div_by)
  119. * when div_by > mul_by
  120. */
  121. static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
  122. {
  123. lba512_t bc_quot, bc_rem;
  124. /* x * m / d == x / d * m + (x % d) * m / d */
  125. bc_quot = block_count / div_by;
  126. bc_rem = block_count - div_by * bc_quot;
  127. return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
  128. }
  129. void dev_print (block_dev_desc_t *dev_desc)
  130. {
  131. lba512_t lba512; /* number of blocks if 512bytes block size */
  132. if (dev_desc->type == DEV_TYPE_UNKNOWN) {
  133. puts ("not available\n");
  134. return;
  135. }
  136. switch (dev_desc->if_type) {
  137. case IF_TYPE_SCSI:
  138. printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
  139. dev_desc->target,dev_desc->lun,
  140. dev_desc->vendor,
  141. dev_desc->product,
  142. dev_desc->revision);
  143. break;
  144. case IF_TYPE_ATAPI:
  145. case IF_TYPE_IDE:
  146. case IF_TYPE_SATA:
  147. printf ("Model: %s Firm: %s Ser#: %s\n",
  148. dev_desc->vendor,
  149. dev_desc->revision,
  150. dev_desc->product);
  151. break;
  152. case IF_TYPE_SD:
  153. case IF_TYPE_MMC:
  154. case IF_TYPE_USB:
  155. printf ("Vendor: %s Rev: %s Prod: %s\n",
  156. dev_desc->vendor,
  157. dev_desc->revision,
  158. dev_desc->product);
  159. break;
  160. case IF_TYPE_DOC:
  161. puts("device type DOC\n");
  162. return;
  163. case IF_TYPE_UNKNOWN:
  164. puts("device type unknown\n");
  165. return;
  166. default:
  167. printf("Unhandled device type: %i\n", dev_desc->if_type);
  168. return;
  169. }
  170. puts (" Type: ");
  171. if (dev_desc->removable)
  172. puts ("Removable ");
  173. switch (dev_desc->type & 0x1F) {
  174. case DEV_TYPE_HARDDISK:
  175. puts ("Hard Disk");
  176. break;
  177. case DEV_TYPE_CDROM:
  178. puts ("CD ROM");
  179. break;
  180. case DEV_TYPE_OPDISK:
  181. puts ("Optical Device");
  182. break;
  183. case DEV_TYPE_TAPE:
  184. puts ("Tape");
  185. break;
  186. default:
  187. printf ("# %02X #", dev_desc->type & 0x1F);
  188. break;
  189. }
  190. puts ("\n");
  191. if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
  192. ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
  193. lbaint_t lba;
  194. lba = dev_desc->lba;
  195. lba512 = (lba * (dev_desc->blksz/512));
  196. /* round to 1 digit */
  197. /* 2048 = (1024 * 1024) / 512 MB */
  198. mb = lba512_muldiv(lba512, 10, 2048);
  199. mb_quot = mb / 10;
  200. mb_rem = mb - (10 * mb_quot);
  201. gb = mb / 1024;
  202. gb_quot = gb / 10;
  203. gb_rem = gb - (10 * gb_quot);
  204. #ifdef CONFIG_LBA48
  205. if (dev_desc->lba48)
  206. printf (" Supports 48-bit addressing\n");
  207. #endif
  208. #if defined(CONFIG_SYS_64BIT_LBA)
  209. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
  210. mb_quot, mb_rem,
  211. gb_quot, gb_rem,
  212. lba,
  213. dev_desc->blksz);
  214. #else
  215. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
  216. mb_quot, mb_rem,
  217. gb_quot, gb_rem,
  218. (ulong)lba,
  219. dev_desc->blksz);
  220. #endif
  221. } else {
  222. puts (" Capacity: not available\n");
  223. }
  224. }
  225. #endif
  226. #ifdef HAVE_BLOCK_DEVICE
  227. void init_part(block_dev_desc_t *dev_desc)
  228. {
  229. #ifdef CONFIG_ISO_PARTITION
  230. if (test_part_iso(dev_desc) == 0) {
  231. dev_desc->part_type = PART_TYPE_ISO;
  232. return;
  233. }
  234. #endif
  235. #ifdef CONFIG_MAC_PARTITION
  236. if (test_part_mac(dev_desc) == 0) {
  237. dev_desc->part_type = PART_TYPE_MAC;
  238. return;
  239. }
  240. #endif
  241. /* must be placed before DOS partition detection */
  242. #ifdef CONFIG_EFI_PARTITION
  243. if (test_part_efi(dev_desc) == 0) {
  244. dev_desc->part_type = PART_TYPE_EFI;
  245. return;
  246. }
  247. #endif
  248. #ifdef CONFIG_DOS_PARTITION
  249. if (test_part_dos(dev_desc) == 0) {
  250. dev_desc->part_type = PART_TYPE_DOS;
  251. return;
  252. }
  253. #endif
  254. #ifdef CONFIG_AMIGA_PARTITION
  255. if (test_part_amiga(dev_desc) == 0) {
  256. dev_desc->part_type = PART_TYPE_AMIGA;
  257. return;
  258. }
  259. #endif
  260. dev_desc->part_type = PART_TYPE_UNKNOWN;
  261. }
  262. #if defined(CONFIG_MAC_PARTITION) || \
  263. defined(CONFIG_DOS_PARTITION) || \
  264. defined(CONFIG_ISO_PARTITION) || \
  265. defined(CONFIG_AMIGA_PARTITION) || \
  266. defined(CONFIG_EFI_PARTITION)
  267. static void print_part_header(const char *type, block_dev_desc_t *dev_desc)
  268. {
  269. puts ("\nPartition Map for ");
  270. switch (dev_desc->if_type) {
  271. case IF_TYPE_IDE:
  272. puts ("IDE");
  273. break;
  274. case IF_TYPE_SATA:
  275. puts ("SATA");
  276. break;
  277. case IF_TYPE_SCSI:
  278. puts ("SCSI");
  279. break;
  280. case IF_TYPE_ATAPI:
  281. puts ("ATAPI");
  282. break;
  283. case IF_TYPE_USB:
  284. puts ("USB");
  285. break;
  286. case IF_TYPE_DOC:
  287. puts ("DOC");
  288. break;
  289. case IF_TYPE_MMC:
  290. puts ("MMC");
  291. break;
  292. case IF_TYPE_HOST:
  293. puts("HOST");
  294. break;
  295. default:
  296. puts ("UNKNOWN");
  297. break;
  298. }
  299. printf (" device %d -- Partition Type: %s\n\n",
  300. dev_desc->dev, type);
  301. }
  302. #endif /* any CONFIG_..._PARTITION */
  303. void print_part(block_dev_desc_t * dev_desc)
  304. {
  305. switch (dev_desc->part_type) {
  306. #ifdef CONFIG_MAC_PARTITION
  307. case PART_TYPE_MAC:
  308. PRINTF ("## Testing for valid MAC partition ##\n");
  309. print_part_header ("MAC", dev_desc);
  310. print_part_mac (dev_desc);
  311. return;
  312. #endif
  313. #ifdef CONFIG_DOS_PARTITION
  314. case PART_TYPE_DOS:
  315. PRINTF ("## Testing for valid DOS partition ##\n");
  316. print_part_header ("DOS", dev_desc);
  317. print_part_dos (dev_desc);
  318. return;
  319. #endif
  320. #ifdef CONFIG_ISO_PARTITION
  321. case PART_TYPE_ISO:
  322. PRINTF ("## Testing for valid ISO Boot partition ##\n");
  323. print_part_header ("ISO", dev_desc);
  324. print_part_iso (dev_desc);
  325. return;
  326. #endif
  327. #ifdef CONFIG_AMIGA_PARTITION
  328. case PART_TYPE_AMIGA:
  329. PRINTF ("## Testing for a valid Amiga partition ##\n");
  330. print_part_header ("AMIGA", dev_desc);
  331. print_part_amiga (dev_desc);
  332. return;
  333. #endif
  334. #ifdef CONFIG_EFI_PARTITION
  335. case PART_TYPE_EFI:
  336. PRINTF ("## Testing for valid EFI partition ##\n");
  337. print_part_header ("EFI", dev_desc);
  338. print_part_efi (dev_desc);
  339. return;
  340. #endif
  341. }
  342. puts ("## Unknown partition table\n");
  343. }
  344. #endif /* HAVE_BLOCK_DEVICE */
  345. int get_partition_info(block_dev_desc_t *dev_desc, int part,
  346. disk_partition_t *info)
  347. {
  348. #ifdef HAVE_BLOCK_DEVICE
  349. #ifdef CONFIG_PARTITION_UUIDS
  350. /* The common case is no UUID support */
  351. info->uuid[0] = 0;
  352. #endif
  353. switch (dev_desc->part_type) {
  354. #ifdef CONFIG_MAC_PARTITION
  355. case PART_TYPE_MAC:
  356. if (get_partition_info_mac(dev_desc, part, info) == 0) {
  357. PRINTF("## Valid MAC partition found ##\n");
  358. return 0;
  359. }
  360. break;
  361. #endif
  362. #ifdef CONFIG_DOS_PARTITION
  363. case PART_TYPE_DOS:
  364. if (get_partition_info_dos(dev_desc, part, info) == 0) {
  365. PRINTF("## Valid DOS partition found ##\n");
  366. return 0;
  367. }
  368. break;
  369. #endif
  370. #ifdef CONFIG_ISO_PARTITION
  371. case PART_TYPE_ISO:
  372. if (get_partition_info_iso(dev_desc, part, info) == 0) {
  373. PRINTF("## Valid ISO boot partition found ##\n");
  374. return 0;
  375. }
  376. break;
  377. #endif
  378. #ifdef CONFIG_AMIGA_PARTITION
  379. case PART_TYPE_AMIGA:
  380. if (get_partition_info_amiga(dev_desc, part, info) == 0) {
  381. PRINTF("## Valid Amiga partition found ##\n");
  382. return 0;
  383. }
  384. break;
  385. #endif
  386. #ifdef CONFIG_EFI_PARTITION
  387. case PART_TYPE_EFI:
  388. if (get_partition_info_efi(dev_desc, part, info) == 0) {
  389. PRINTF("## Valid EFI partition found ##\n");
  390. return 0;
  391. }
  392. break;
  393. #endif
  394. default:
  395. break;
  396. }
  397. #endif /* HAVE_BLOCK_DEVICE */
  398. return -1;
  399. }
  400. int get_device(const char *ifname, const char *dev_hwpart_str,
  401. block_dev_desc_t **dev_desc)
  402. {
  403. char *ep;
  404. char *dup_str = NULL;
  405. const char *dev_str, *hwpart_str;
  406. int dev, hwpart;
  407. hwpart_str = strchr(dev_hwpart_str, '.');
  408. if (hwpart_str) {
  409. dup_str = strdup(dev_hwpart_str);
  410. dup_str[hwpart_str - dev_hwpart_str] = 0;
  411. dev_str = dup_str;
  412. hwpart_str++;
  413. } else {
  414. dev_str = dev_hwpart_str;
  415. hwpart = 0;
  416. }
  417. dev = simple_strtoul(dev_str, &ep, 16);
  418. if (*ep) {
  419. printf("** Bad device specification %s %s **\n",
  420. ifname, dev_str);
  421. dev = -1;
  422. goto cleanup;
  423. }
  424. if (hwpart_str) {
  425. hwpart = simple_strtoul(hwpart_str, &ep, 16);
  426. if (*ep) {
  427. printf("** Bad HW partition specification %s %s **\n",
  428. ifname, hwpart_str);
  429. dev = -1;
  430. goto cleanup;
  431. }
  432. }
  433. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  434. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  435. printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  436. dev = -1;
  437. goto cleanup;
  438. }
  439. cleanup:
  440. free(dup_str);
  441. return dev;
  442. }
  443. #define PART_UNSPECIFIED -2
  444. #define PART_AUTO -1
  445. #define MAX_SEARCH_PARTITIONS 16
  446. int get_device_and_partition(const char *ifname, const char *dev_part_str,
  447. block_dev_desc_t **dev_desc,
  448. disk_partition_t *info, int allow_whole_dev)
  449. {
  450. int ret = -1;
  451. const char *part_str;
  452. char *dup_str = NULL;
  453. const char *dev_str;
  454. int dev;
  455. char *ep;
  456. int p;
  457. int part;
  458. disk_partition_t tmpinfo;
  459. /*
  460. * Special-case a pseudo block device "hostfs", to allow access to the
  461. * host's own filesystem.
  462. */
  463. if (0 == strcmp(ifname, "hostfs")) {
  464. *dev_desc = NULL;
  465. info->start = 0;
  466. info->size = 0;
  467. info->blksz = 0;
  468. info->bootable = 0;
  469. strcpy((char *)info->type, BOOT_PART_TYPE);
  470. strcpy((char *)info->name, "Sandbox host");
  471. #ifdef CONFIG_PARTITION_UUIDS
  472. info->uuid[0] = 0;
  473. #endif
  474. return 0;
  475. }
  476. /* If no dev_part_str, use bootdevice environment variable */
  477. if (!dev_part_str || !strlen(dev_part_str) ||
  478. !strcmp(dev_part_str, "-"))
  479. dev_part_str = getenv("bootdevice");
  480. /* If still no dev_part_str, it's an error */
  481. if (!dev_part_str) {
  482. printf("** No device specified **\n");
  483. goto cleanup;
  484. }
  485. /* Separate device and partition ID specification */
  486. part_str = strchr(dev_part_str, ':');
  487. if (part_str) {
  488. dup_str = strdup(dev_part_str);
  489. dup_str[part_str - dev_part_str] = 0;
  490. dev_str = dup_str;
  491. part_str++;
  492. } else {
  493. dev_str = dev_part_str;
  494. }
  495. /* Look up the device */
  496. dev = get_device(ifname, dev_str, dev_desc);
  497. if (dev < 0)
  498. goto cleanup;
  499. /* Convert partition ID string to number */
  500. if (!part_str || !*part_str) {
  501. part = PART_UNSPECIFIED;
  502. } else if (!strcmp(part_str, "auto")) {
  503. part = PART_AUTO;
  504. } else {
  505. /* Something specified -> use exactly that */
  506. part = (int)simple_strtoul(part_str, &ep, 16);
  507. /*
  508. * Less than whole string converted,
  509. * or request for whole device, but caller requires partition.
  510. */
  511. if (*ep || (part == 0 && !allow_whole_dev)) {
  512. printf("** Bad partition specification %s %s **\n",
  513. ifname, dev_part_str);
  514. goto cleanup;
  515. }
  516. }
  517. /*
  518. * No partition table on device,
  519. * or user requested partition 0 (entire device).
  520. */
  521. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  522. (part == 0)) {
  523. if (!(*dev_desc)->lba) {
  524. printf("** Bad device size - %s %s **\n", ifname,
  525. dev_str);
  526. goto cleanup;
  527. }
  528. /*
  529. * If user specified a partition ID other than 0,
  530. * or the calling command only accepts partitions,
  531. * it's an error.
  532. */
  533. if ((part > 0) || (!allow_whole_dev)) {
  534. printf("** No partition table - %s %s **\n", ifname,
  535. dev_str);
  536. goto cleanup;
  537. }
  538. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  539. info->start = 0;
  540. info->size = (*dev_desc)->lba;
  541. info->blksz = (*dev_desc)->blksz;
  542. info->bootable = 0;
  543. strcpy((char *)info->type, BOOT_PART_TYPE);
  544. strcpy((char *)info->name, "Whole Disk");
  545. #ifdef CONFIG_PARTITION_UUIDS
  546. info->uuid[0] = 0;
  547. #endif
  548. ret = 0;
  549. goto cleanup;
  550. }
  551. /*
  552. * Now there's known to be a partition table,
  553. * not specifying a partition means to pick partition 1.
  554. */
  555. if (part == PART_UNSPECIFIED)
  556. part = 1;
  557. /*
  558. * If user didn't specify a partition number, or did specify something
  559. * other than "auto", use that partition number directly.
  560. */
  561. if (part != PART_AUTO) {
  562. ret = get_partition_info(*dev_desc, part, info);
  563. if (ret) {
  564. printf("** Invalid partition %d **\n", part);
  565. goto cleanup;
  566. }
  567. } else {
  568. /*
  569. * Find the first bootable partition.
  570. * If none are bootable, fall back to the first valid partition.
  571. */
  572. part = 0;
  573. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  574. ret = get_partition_info(*dev_desc, p, info);
  575. if (ret)
  576. continue;
  577. /*
  578. * First valid partition, or new better partition?
  579. * If so, save partition ID.
  580. */
  581. if (!part || info->bootable)
  582. part = p;
  583. /* Best possible partition? Stop searching. */
  584. if (info->bootable)
  585. break;
  586. /*
  587. * We now need to search further for best possible.
  588. * If we what we just queried was the best so far,
  589. * save the info since we over-write it next loop.
  590. */
  591. if (part == p)
  592. tmpinfo = *info;
  593. }
  594. /* If we found any acceptable partition */
  595. if (part) {
  596. /*
  597. * If we searched all possible partition IDs,
  598. * return the first valid partition we found.
  599. */
  600. if (p == MAX_SEARCH_PARTITIONS + 1)
  601. *info = tmpinfo;
  602. } else {
  603. printf("** No valid partitions found **\n");
  604. ret = -1;
  605. goto cleanup;
  606. }
  607. }
  608. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  609. printf("** Invalid partition type \"%.32s\""
  610. " (expect \"" BOOT_PART_TYPE "\")\n",
  611. info->type);
  612. ret = -1;
  613. goto cleanup;
  614. }
  615. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  616. ret = part;
  617. goto cleanup;
  618. cleanup:
  619. free(dup_str);
  620. return ret;
  621. }