cmdline.c 2.2 KB

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