cmdline.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 HUAWEI
  4. * Author: Cai Zhiyong <caizhiyong@huawei.com>
  5. *
  6. * Read block device partition table from the command line.
  7. * Typically used for fixed block (eMMC) embedded devices.
  8. * It has no MBR, so saves storage space. Bootloader can be easily accessed
  9. * by absolute address of data on the block device.
  10. * Users can easily change the partition.
  11. *
  12. * The format for the command line is just like mtdparts.
  13. *
  14. * For further information, see "Documentation/block/cmdline-partition.txt"
  15. *
  16. */
  17. #include <linux/cmdline-parser.h>
  18. #include "check.h"
  19. #include "cmdline.h"
  20. static char *cmdline;
  21. static struct cmdline_parts *bdev_parts;
  22. static int add_part(int slot, struct cmdline_subpart *subpart, void *param)
  23. {
  24. int label_min;
  25. struct partition_meta_info *info;
  26. char tmp[sizeof(info->volname) + 4];
  27. struct parsed_partitions *state = (struct parsed_partitions *)param;
  28. if (slot >= state->limit)
  29. return 1;
  30. put_partition(state, slot, subpart->from >> 9,
  31. subpart->size >> 9);
  32. info = &state->parts[slot].info;
  33. label_min = min_t(int, sizeof(info->volname) - 1,
  34. sizeof(subpart->name));
  35. strncpy(info->volname, subpart->name, label_min);
  36. info->volname[label_min] = '\0';
  37. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  38. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  39. state->parts[slot].has_info = true;
  40. return 0;
  41. }
  42. static int __init cmdline_parts_setup(char *s)
  43. {
  44. cmdline = s;
  45. return 1;
  46. }
  47. __setup("blkdevparts=", cmdline_parts_setup);
  48. /*
  49. * Purpose: allocate cmdline partitions.
  50. * Returns:
  51. * -1 if unable to read the partition table
  52. * 0 if this isn't our partition table
  53. * 1 if successful
  54. */
  55. int cmdline_partition(struct parsed_partitions *state)
  56. {
  57. sector_t disk_size;
  58. char bdev[BDEVNAME_SIZE];
  59. struct cmdline_parts *parts;
  60. if (cmdline) {
  61. if (bdev_parts)
  62. cmdline_parts_free(&bdev_parts);
  63. if (cmdline_parts_parse(&bdev_parts, cmdline)) {
  64. cmdline = NULL;
  65. return -1;
  66. }
  67. cmdline = NULL;
  68. }
  69. if (!bdev_parts)
  70. return 0;
  71. bdevname(state->bdev, bdev);
  72. parts = cmdline_parts_find(bdev_parts, bdev);
  73. if (!parts)
  74. return 0;
  75. disk_size = get_capacity(state->bdev->bd_disk) << 9;
  76. cmdline_parts_set(parts, disk_size, 1, add_part, (void *)state);
  77. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  78. return 1;
  79. }