part.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. 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. mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
  198. mb_quot = mb / 10;
  199. mb_rem = mb - (10 * mb_quot);
  200. gb = mb / 1024;
  201. gb_quot = gb / 10;
  202. gb_rem = gb - (10 * gb_quot);
  203. #ifdef CONFIG_LBA48
  204. if (dev_desc->lba48)
  205. printf (" Supports 48-bit addressing\n");
  206. #endif
  207. #if defined(CONFIG_SYS_64BIT_LBA)
  208. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
  209. mb_quot, mb_rem,
  210. gb_quot, gb_rem,
  211. lba,
  212. dev_desc->blksz);
  213. #else
  214. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
  215. mb_quot, mb_rem,
  216. gb_quot, gb_rem,
  217. (ulong)lba,
  218. dev_desc->blksz);
  219. #endif
  220. } else {
  221. puts (" Capacity: not available\n");
  222. }
  223. }
  224. #endif
  225. #ifdef HAVE_BLOCK_DEVICE
  226. void init_part (block_dev_desc_t * dev_desc)
  227. {
  228. #ifdef CONFIG_ISO_PARTITION
  229. if (test_part_iso(dev_desc) == 0) {
  230. dev_desc->part_type = PART_TYPE_ISO;
  231. return;
  232. }
  233. #endif
  234. #ifdef CONFIG_MAC_PARTITION
  235. if (test_part_mac(dev_desc) == 0) {
  236. dev_desc->part_type = PART_TYPE_MAC;
  237. return;
  238. }
  239. #endif
  240. /* must be placed before DOS partition detection */
  241. #ifdef CONFIG_EFI_PARTITION
  242. if (test_part_efi(dev_desc) == 0) {
  243. dev_desc->part_type = PART_TYPE_EFI;
  244. return;
  245. }
  246. #endif
  247. #ifdef CONFIG_DOS_PARTITION
  248. if (test_part_dos(dev_desc) == 0) {
  249. dev_desc->part_type = PART_TYPE_DOS;
  250. return;
  251. }
  252. #endif
  253. #ifdef CONFIG_AMIGA_PARTITION
  254. if (test_part_amiga(dev_desc) == 0) {
  255. dev_desc->part_type = PART_TYPE_AMIGA;
  256. return;
  257. }
  258. #endif
  259. dev_desc->part_type = PART_TYPE_UNKNOWN;
  260. }
  261. #if defined(CONFIG_MAC_PARTITION) || \
  262. defined(CONFIG_DOS_PARTITION) || \
  263. defined(CONFIG_ISO_PARTITION) || \
  264. defined(CONFIG_AMIGA_PARTITION) || \
  265. defined(CONFIG_EFI_PARTITION)
  266. static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
  267. {
  268. puts ("\nPartition Map for ");
  269. switch (dev_desc->if_type) {
  270. case IF_TYPE_IDE:
  271. puts ("IDE");
  272. break;
  273. case IF_TYPE_SATA:
  274. puts ("SATA");
  275. break;
  276. case IF_TYPE_SCSI:
  277. puts ("SCSI");
  278. break;
  279. case IF_TYPE_ATAPI:
  280. puts ("ATAPI");
  281. break;
  282. case IF_TYPE_USB:
  283. puts ("USB");
  284. break;
  285. case IF_TYPE_DOC:
  286. puts ("DOC");
  287. break;
  288. case IF_TYPE_MMC:
  289. puts ("MMC");
  290. break;
  291. case IF_TYPE_HOST:
  292. puts("HOST");
  293. break;
  294. default:
  295. puts ("UNKNOWN");
  296. break;
  297. }
  298. printf (" device %d -- Partition Type: %s\n\n",
  299. dev_desc->dev, type);
  300. }
  301. #endif /* any CONFIG_..._PARTITION */
  302. void print_part(block_dev_desc_t * dev_desc)
  303. {
  304. switch (dev_desc->part_type) {
  305. #ifdef CONFIG_MAC_PARTITION
  306. case PART_TYPE_MAC:
  307. PRINTF ("## Testing for valid MAC partition ##\n");
  308. print_part_header ("MAC", dev_desc);
  309. print_part_mac (dev_desc);
  310. return;
  311. #endif
  312. #ifdef CONFIG_DOS_PARTITION
  313. case PART_TYPE_DOS:
  314. PRINTF ("## Testing for valid DOS partition ##\n");
  315. print_part_header ("DOS", dev_desc);
  316. print_part_dos (dev_desc);
  317. return;
  318. #endif
  319. #ifdef CONFIG_ISO_PARTITION
  320. case PART_TYPE_ISO:
  321. PRINTF ("## Testing for valid ISO Boot partition ##\n");
  322. print_part_header ("ISO", dev_desc);
  323. print_part_iso (dev_desc);
  324. return;
  325. #endif
  326. #ifdef CONFIG_AMIGA_PARTITION
  327. case PART_TYPE_AMIGA:
  328. PRINTF ("## Testing for a valid Amiga partition ##\n");
  329. print_part_header ("AMIGA", dev_desc);
  330. print_part_amiga (dev_desc);
  331. return;
  332. #endif
  333. #ifdef CONFIG_EFI_PARTITION
  334. case PART_TYPE_EFI:
  335. PRINTF ("## Testing for valid EFI partition ##\n");
  336. print_part_header ("EFI", dev_desc);
  337. print_part_efi (dev_desc);
  338. return;
  339. #endif
  340. }
  341. puts ("## Unknown partition table\n");
  342. }
  343. #endif /* HAVE_BLOCK_DEVICE */
  344. int get_partition_info(block_dev_desc_t *dev_desc, int part,
  345. disk_partition_t *info)
  346. {
  347. #ifdef HAVE_BLOCK_DEVICE
  348. #ifdef CONFIG_PARTITION_UUIDS
  349. /* The common case is no UUID support */
  350. info->uuid[0] = 0;
  351. #endif
  352. switch (dev_desc->part_type) {
  353. #ifdef CONFIG_MAC_PARTITION
  354. case PART_TYPE_MAC:
  355. if (get_partition_info_mac(dev_desc, part, info) == 0) {
  356. PRINTF("## Valid MAC partition found ##\n");
  357. return 0;
  358. }
  359. break;
  360. #endif
  361. #ifdef CONFIG_DOS_PARTITION
  362. case PART_TYPE_DOS:
  363. if (get_partition_info_dos(dev_desc, part, info) == 0) {
  364. PRINTF("## Valid DOS partition found ##\n");
  365. return 0;
  366. }
  367. break;
  368. #endif
  369. #ifdef CONFIG_ISO_PARTITION
  370. case PART_TYPE_ISO:
  371. if (get_partition_info_iso(dev_desc, part, info) == 0) {
  372. PRINTF("## Valid ISO boot partition found ##\n");
  373. return 0;
  374. }
  375. break;
  376. #endif
  377. #ifdef CONFIG_AMIGA_PARTITION
  378. case PART_TYPE_AMIGA:
  379. if (get_partition_info_amiga(dev_desc, part, info) == 0) {
  380. PRINTF("## Valid Amiga partition found ##\n");
  381. return 0;
  382. }
  383. break;
  384. #endif
  385. #ifdef CONFIG_EFI_PARTITION
  386. case PART_TYPE_EFI:
  387. if (get_partition_info_efi(dev_desc, part, info) == 0) {
  388. PRINTF("## Valid EFI partition found ##\n");
  389. return 0;
  390. }
  391. break;
  392. #endif
  393. default:
  394. break;
  395. }
  396. #endif /* HAVE_BLOCK_DEVICE */
  397. return -1;
  398. }
  399. int get_device(const char *ifname, const char *dev_hwpart_str,
  400. block_dev_desc_t **dev_desc)
  401. {
  402. char *ep;
  403. char *dup_str = NULL;
  404. const char *dev_str, *hwpart_str;
  405. int dev, hwpart;
  406. hwpart_str = strchr(dev_hwpart_str, '.');
  407. if (hwpart_str) {
  408. dup_str = strdup(dev_hwpart_str);
  409. dup_str[hwpart_str - dev_hwpart_str] = 0;
  410. dev_str = dup_str;
  411. hwpart_str++;
  412. } else {
  413. dev_str = dev_hwpart_str;
  414. hwpart = 0;
  415. }
  416. dev = simple_strtoul(dev_str, &ep, 16);
  417. if (*ep) {
  418. printf("** Bad device specification %s %s **\n",
  419. ifname, dev_str);
  420. dev = -1;
  421. goto cleanup;
  422. }
  423. if (hwpart_str) {
  424. hwpart = simple_strtoul(hwpart_str, &ep, 16);
  425. if (*ep) {
  426. printf("** Bad HW partition specification %s %s **\n",
  427. ifname, hwpart_str);
  428. dev = -1;
  429. goto cleanup;
  430. }
  431. }
  432. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  433. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  434. printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  435. dev = -1;
  436. goto cleanup;
  437. }
  438. cleanup:
  439. free(dup_str);
  440. return dev;
  441. }
  442. #define PART_UNSPECIFIED -2
  443. #define PART_AUTO -1
  444. #define MAX_SEARCH_PARTITIONS 16
  445. int get_device_and_partition(const char *ifname, const char *dev_part_str,
  446. block_dev_desc_t **dev_desc,
  447. disk_partition_t *info, int allow_whole_dev)
  448. {
  449. int ret = -1;
  450. const char *part_str;
  451. char *dup_str = NULL;
  452. const char *dev_str;
  453. int dev;
  454. char *ep;
  455. int p;
  456. int part;
  457. disk_partition_t tmpinfo;
  458. /*
  459. * Special-case a pseudo block device "hostfs", to allow access to the
  460. * host's own filesystem.
  461. */
  462. if (0 == strcmp(ifname, "hostfs")) {
  463. *dev_desc = NULL;
  464. info->start = 0;
  465. info->size = 0;
  466. info->blksz = 0;
  467. info->bootable = 0;
  468. strcpy((char *)info->type, BOOT_PART_TYPE);
  469. strcpy((char *)info->name, "Sandbox host");
  470. #ifdef CONFIG_PARTITION_UUIDS
  471. info->uuid[0] = 0;
  472. #endif
  473. return 0;
  474. }
  475. /* If no dev_part_str, use bootdevice environment variable */
  476. if (!dev_part_str || !strlen(dev_part_str) ||
  477. !strcmp(dev_part_str, "-"))
  478. dev_part_str = getenv("bootdevice");
  479. /* If still no dev_part_str, it's an error */
  480. if (!dev_part_str) {
  481. printf("** No device specified **\n");
  482. goto cleanup;
  483. }
  484. /* Separate device and partition ID specification */
  485. part_str = strchr(dev_part_str, ':');
  486. if (part_str) {
  487. dup_str = strdup(dev_part_str);
  488. dup_str[part_str - dev_part_str] = 0;
  489. dev_str = dup_str;
  490. part_str++;
  491. } else {
  492. dev_str = dev_part_str;
  493. }
  494. /* Look up the device */
  495. dev = get_device(ifname, dev_str, dev_desc);
  496. if (dev < 0)
  497. goto cleanup;
  498. /* Convert partition ID string to number */
  499. if (!part_str || !*part_str) {
  500. part = PART_UNSPECIFIED;
  501. } else if (!strcmp(part_str, "auto")) {
  502. part = PART_AUTO;
  503. } else {
  504. /* Something specified -> use exactly that */
  505. part = (int)simple_strtoul(part_str, &ep, 16);
  506. /*
  507. * Less than whole string converted,
  508. * or request for whole device, but caller requires partition.
  509. */
  510. if (*ep || (part == 0 && !allow_whole_dev)) {
  511. printf("** Bad partition specification %s %s **\n",
  512. ifname, dev_part_str);
  513. goto cleanup;
  514. }
  515. }
  516. /*
  517. * No partition table on device,
  518. * or user requested partition 0 (entire device).
  519. */
  520. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  521. (part == 0)) {
  522. if (!(*dev_desc)->lba) {
  523. printf("** Bad device size - %s %s **\n", ifname,
  524. dev_str);
  525. goto cleanup;
  526. }
  527. /*
  528. * If user specified a partition ID other than 0,
  529. * or the calling command only accepts partitions,
  530. * it's an error.
  531. */
  532. if ((part > 0) || (!allow_whole_dev)) {
  533. printf("** No partition table - %s %s **\n", ifname,
  534. dev_str);
  535. goto cleanup;
  536. }
  537. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  538. info->start = 0;
  539. info->size = (*dev_desc)->lba;
  540. info->blksz = (*dev_desc)->blksz;
  541. info->bootable = 0;
  542. strcpy((char *)info->type, BOOT_PART_TYPE);
  543. strcpy((char *)info->name, "Whole Disk");
  544. #ifdef CONFIG_PARTITION_UUIDS
  545. info->uuid[0] = 0;
  546. #endif
  547. ret = 0;
  548. goto cleanup;
  549. }
  550. /*
  551. * Now there's known to be a partition table,
  552. * not specifying a partition means to pick partition 1.
  553. */
  554. if (part == PART_UNSPECIFIED)
  555. part = 1;
  556. /*
  557. * If user didn't specify a partition number, or did specify something
  558. * other than "auto", use that partition number directly.
  559. */
  560. if (part != PART_AUTO) {
  561. ret = get_partition_info(*dev_desc, part, info);
  562. if (ret) {
  563. printf("** Invalid partition %d **\n", part);
  564. goto cleanup;
  565. }
  566. } else {
  567. /*
  568. * Find the first bootable partition.
  569. * If none are bootable, fall back to the first valid partition.
  570. */
  571. part = 0;
  572. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  573. ret = get_partition_info(*dev_desc, p, info);
  574. if (ret)
  575. continue;
  576. /*
  577. * First valid partition, or new better partition?
  578. * If so, save partition ID.
  579. */
  580. if (!part || info->bootable)
  581. part = p;
  582. /* Best possible partition? Stop searching. */
  583. if (info->bootable)
  584. break;
  585. /*
  586. * We now need to search further for best possible.
  587. * If we what we just queried was the best so far,
  588. * save the info since we over-write it next loop.
  589. */
  590. if (part == p)
  591. tmpinfo = *info;
  592. }
  593. /* If we found any acceptable partition */
  594. if (part) {
  595. /*
  596. * If we searched all possible partition IDs,
  597. * return the first valid partition we found.
  598. */
  599. if (p == MAX_SEARCH_PARTITIONS + 1)
  600. *info = tmpinfo;
  601. } else {
  602. printf("** No valid partitions found **\n");
  603. ret = -1;
  604. goto cleanup;
  605. }
  606. }
  607. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  608. printf("** Invalid partition type \"%.32s\""
  609. " (expect \"" BOOT_PART_TYPE "\")\n",
  610. info->type);
  611. ret = -1;
  612. goto cleanup;
  613. }
  614. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  615. ret = part;
  616. goto cleanup;
  617. cleanup:
  618. free(dup_str);
  619. return ret;
  620. }