dospart.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. */
  6. #include <kernel/system.h>
  7. #include <kernel/logging.h>
  8. #include <kernel/module.h>
  9. #include <kernel/printf.h>
  10. #include <kernel/ata.h>
  11. #define SECTORSIZE 512
  12. static mbr_t mbr;
  13. struct dos_partition_entry {
  14. fs_node_t * device;
  15. partition_t partition;
  16. };
  17. static uint32_t read_part(fs_node_t *node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  18. struct dos_partition_entry * device = (struct dos_partition_entry *)node->device;
  19. if (offset > device->partition.sector_count * SECTORSIZE) {
  20. debug_print(WARNING, "Read beyond partition!");
  21. return 0;
  22. }
  23. if (offset + size > device->partition.sector_count * SECTORSIZE) {
  24. size = device->partition.sector_count * SECTORSIZE - offset;
  25. debug_print(WARNING, "Tried to read past end of partition, clamped to %d", size);
  26. }
  27. return read_fs(device->device, offset + device->partition.lba_first_sector * SECTORSIZE, size, buffer);
  28. }
  29. static uint32_t write_part(fs_node_t *node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  30. struct dos_partition_entry * device = (struct dos_partition_entry *)node->device;
  31. if (offset > device->partition.sector_count * SECTORSIZE) {
  32. return 0;
  33. }
  34. if (offset + size > device->partition.sector_count * SECTORSIZE) {
  35. size = device->partition.sector_count * SECTORSIZE - offset;
  36. }
  37. return write_fs(device->device, offset + device->partition.lba_first_sector * SECTORSIZE, size, buffer);
  38. }
  39. static void open_part(fs_node_t * node, unsigned int flags) {
  40. return;
  41. }
  42. static void close_part(fs_node_t * node) {
  43. return;
  44. }
  45. static fs_node_t * dospart_device_create(int i, fs_node_t * dev, partition_t * part) {
  46. vfs_lock(dev);
  47. struct dos_partition_entry * device = malloc(sizeof(struct dos_partition_entry));
  48. memcpy(&device->partition, part, sizeof(partition_t));
  49. device->device = dev;
  50. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  51. memset(fnode, 0x00, sizeof(fs_node_t));
  52. fnode->inode = 0;
  53. sprintf(fnode->name, "dospart%d", i);
  54. fnode->device = device;
  55. fnode->uid = 0;
  56. fnode->gid = 0;
  57. fnode->mask = 0660;
  58. fnode->length = device->partition.sector_count * SECTORSIZE; /* TODO */
  59. fnode->flags = FS_BLOCKDEVICE;
  60. fnode->read = read_part;
  61. fnode->write = write_part;
  62. fnode->open = open_part;
  63. fnode->close = close_part;
  64. fnode->readdir = NULL;
  65. fnode->finddir = NULL;
  66. fnode->ioctl = NULL; /* TODO, identify, etc? */
  67. return fnode;
  68. }
  69. static int read_partition_map(char * name) {
  70. fs_node_t * device = kopen(name, 0);
  71. if (!device) return 1;
  72. read_fs(device, 0, SECTORSIZE, (uint8_t *)&mbr);
  73. if (mbr.signature[0] == 0x55 && mbr.signature[1] == 0xAA) {
  74. debug_print(INFO, "Partition table found.");
  75. for (int i = 0; i < 4; ++i) {
  76. if (mbr.partitions[i].status & 0x80) {
  77. debug_print(NOTICE, "Partition #%d: @%d+%d", i+1, mbr.partitions[i].lba_first_sector, mbr.partitions[i].sector_count);
  78. fs_node_t * node = dospart_device_create(i, device, &mbr.partitions[i]);
  79. char tmp[64];
  80. sprintf(tmp, "%s%d", name, i);
  81. vfs_mount(tmp, node);
  82. } else {
  83. debug_print(NOTICE, "Partition #%d: inactive", i+1);
  84. }
  85. }
  86. } else {
  87. debug_print(NOTICE, "No partition table on %s", name);
  88. }
  89. return 0;
  90. }
  91. static int dospart_initialize(void) {
  92. for (char l = 'a'; l < 'z'; ++l) {
  93. char name[64];
  94. sprintf(name, "/dev/hd%c", l);
  95. if (read_partition_map(name)) {
  96. break;
  97. }
  98. }
  99. return 0;
  100. }
  101. static int dospart_finalize(void) {
  102. return 0;
  103. }
  104. MODULE_DEF(dospart, dospart_initialize, dospart_finalize);