blkdev.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Block device registry: floppy (via FDC) + IDE (via ATA). */
  2. #include "blkdev.h"
  3. #include "ata.h"
  4. #include "disk.h"
  5. blk_dev_t blk_devs[BLKDEV_MAX];
  6. unsigned int blk_dev_count;
  7. static blk_dev_t *add(const char *name, blk_kind_t kind,
  8. unsigned int bus, unsigned int unit)
  9. {
  10. blk_dev_t *d;
  11. if (blk_dev_count >= BLKDEV_MAX)
  12. return 0;
  13. d = &blk_devs[blk_dev_count++];
  14. d->name = name;
  15. d->kind = kind;
  16. d->bus = bus;
  17. d->unit = unit;
  18. d->present = 0;
  19. d->total_sectors = 0;
  20. d->model[0] = '\0';
  21. return d;
  22. }
  23. void blk_scan(void)
  24. {
  25. blk_dev_t *d;
  26. blk_dev_count = 0;
  27. /* Floppy drives 0 and 1. */
  28. d = add("floppy0", BLK_FLOPPY, 0, 0);
  29. if (d) { d->present = 1; d->total_sectors = 2880; }
  30. d = add("floppy1", BLK_FLOPPY, 1, 0);
  31. if (d) { d->present = 1; d->total_sectors = 2880; }
  32. /* IDE: primary + secondary, master + slave. */
  33. {
  34. static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
  35. unsigned int i;
  36. for (i = 0; i < 4; i++) {
  37. unsigned int sectors = 0;
  38. char model[41];
  39. d = add(names[i], BLK_IDE, i / 2, i % 2);
  40. if (!d)
  41. continue;
  42. if (ata_probe(i / 2, i % 2, &sectors, model) == 0) {
  43. unsigned int j;
  44. d->present = 1;
  45. d->total_sectors = sectors;
  46. for (j = 0; j < 40; j++)
  47. d->model[j] = model[j];
  48. d->model[40] = '\0';
  49. }
  50. }
  51. }
  52. }
  53. static int name_eq(const char *a, const char *b)
  54. {
  55. while (*a && *b) {
  56. if (*a != *b)
  57. return 0;
  58. a++;
  59. b++;
  60. }
  61. return (*a == *b);
  62. }
  63. blk_dev_t *blk_find(const char *name)
  64. {
  65. unsigned int i;
  66. for (i = 0; i < blk_dev_count; i++)
  67. if (name_eq(blk_devs[i].name, name))
  68. return &blk_devs[i];
  69. return 0;
  70. }
  71. int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
  72. {
  73. if (!d || count == 0)
  74. return -1;
  75. if (d->kind == BLK_FLOPPY) {
  76. disk_set_drive(d->bus);
  77. return disk_read_sectors(lba, buf, count);
  78. }
  79. return ata_read_sectors(d->bus, d->unit, lba, buf, count);
  80. }
  81. int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int count)
  82. {
  83. if (!d || count == 0)
  84. return -1;
  85. if (d->kind == BLK_FLOPPY) {
  86. disk_set_drive(d->bus);
  87. return disk_write_sectors(lba, buf, count);
  88. }
  89. return ata_write_sectors(d->bus, d->unit, lba, buf, count);
  90. }