partition.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <printf.h>
  25. #include <string.h>
  26. #include <compiler.h>
  27. #include <stdlib.h>
  28. #include <arch.h>
  29. #include <lib/bio.h>
  30. #include <lib/partition.h>
  31. struct chs {
  32. uint8_t c;
  33. uint8_t h;
  34. uint8_t s;
  35. } __PACKED;
  36. struct mbr_part {
  37. uint8_t status;
  38. struct chs start;
  39. uint8_t type;
  40. struct chs end;
  41. uint32_t lba_start;
  42. uint32_t lba_length;
  43. } __PACKED;
  44. static status_t validate_mbr_partition(bdev_t *dev, const struct mbr_part *part)
  45. {
  46. /* check for invalid types */
  47. if (part->type == 0)
  48. return -1;
  49. /* check for invalid status */
  50. if (part->status != 0x80 && part->status != 0x00)
  51. return -1;
  52. /* make sure the range fits within the device */
  53. if (part->lba_start >= dev->block_count)
  54. return -1;
  55. if ((part->lba_start + part->lba_length) > dev->block_count)
  56. return -1;
  57. /* that's about all we can do, MBR has no other good way to see if it's valid */
  58. return 0;
  59. }
  60. int partition_publish(const char *device, off_t offset)
  61. {
  62. int err = 0;
  63. int count = 0;
  64. // clear any partitions that may have already existed
  65. partition_unpublish(device);
  66. bdev_t *dev = bio_open(device);
  67. if (!dev) {
  68. printf("partition_publish: unable to open device\n");
  69. return -1;
  70. }
  71. // get a dma aligned and padded block to read info
  72. STACKBUF_DMA_ALIGN(buf, dev->block_size);
  73. /* sniff for MBR partition types */
  74. do {
  75. int i;
  76. err = bio_read(dev, buf, offset, 512);
  77. if (err < 0)
  78. goto err;
  79. /* look for the aa55 tag */
  80. if (buf[510] != 0x55 || buf[511] != 0xaa)
  81. break;
  82. /* see if a partition table makes sense here */
  83. struct mbr_part part[4];
  84. memcpy(part, buf + 446, sizeof(part));
  85. #if DEBUGLEVEL >= INFO
  86. dprintf(INFO, "mbr partition table dump:\n");
  87. for (i=0; i < 4; i++) {
  88. dprintf(INFO, "\t%i: status 0x%hhx, type 0x%hhx, start 0x%x, len 0x%x\n", i, part[i].status, part[i].type, part[i].lba_start, part[i].lba_length);
  89. }
  90. #endif
  91. /* validate each of the partition entries */
  92. for (i=0; i < 4; i++) {
  93. if (validate_mbr_partition(dev, &part[i]) >= 0) {
  94. // publish it
  95. char subdevice[128];
  96. sprintf(subdevice, "%sp%d", device, i);
  97. err = bio_publish_subdevice(device, subdevice, part[i].lba_start, part[i].lba_length);
  98. if (err < 0) {
  99. dprintf(INFO, "error publishing subdevice '%s'\n", subdevice);
  100. continue;
  101. }
  102. count++;
  103. }
  104. }
  105. } while(0);
  106. bio_close(dev);
  107. err:
  108. return (err < 0) ? err : count;
  109. }
  110. int partition_unpublish(const char *device)
  111. {
  112. int i;
  113. int count;
  114. bdev_t *dev;
  115. char devname[512];
  116. count = 0;
  117. for (i=0; i < 16; i++) {
  118. sprintf(devname, "%sp%d", device, i);
  119. dev = bio_open(devname);
  120. if (!dev)
  121. continue;
  122. bio_unregister_device(dev);
  123. bio_close(dev);
  124. count++;
  125. }
  126. return count;
  127. }