blkdev.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 drive 0 (the FDC layer is single-drive). */
  28. d = add("floppy0", BLK_FLOPPY, 0, 0);
  29. if (d) { d->present = 1; d->total_sectors = 2880; }
  30. /* IDE: primary + secondary, master + slave. */
  31. {
  32. static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
  33. unsigned int i;
  34. for (i = 0; i < 4; i++) {
  35. unsigned int sectors = 0;
  36. char model[41];
  37. d = add(names[i], BLK_IDE, i / 2, i % 2);
  38. if (!d)
  39. continue;
  40. if (ata_probe(i / 2, i % 2, &sectors, model) == 0) {
  41. unsigned int j;
  42. d->present = 1;
  43. d->total_sectors = sectors;
  44. for (j = 0; j < 40; j++)
  45. d->model[j] = model[j];
  46. d->model[40] = '\0';
  47. }
  48. }
  49. }
  50. }
  51. static int name_eq(const char *a, const char *b)
  52. {
  53. while (*a && *b) {
  54. if (*a != *b)
  55. return 0;
  56. a++;
  57. b++;
  58. }
  59. return (*a == *b);
  60. }
  61. blk_dev_t *blk_find(const char *name)
  62. {
  63. unsigned int i;
  64. for (i = 0; i < blk_dev_count; i++)
  65. if (name_eq(blk_devs[i].name, name))
  66. return &blk_devs[i];
  67. return 0;
  68. }
  69. int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
  70. {
  71. if (!d || count == 0)
  72. return -1;
  73. if (d->kind == BLK_FLOPPY)
  74. return disk_read_sectors(lba, buf, count);
  75. return ata_read_sectors(d->bus, d->unit, lba, buf, count);
  76. }
  77. int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int count)
  78. {
  79. if (!d || count == 0)
  80. return -1;
  81. if (d->kind == BLK_FLOPPY)
  82. return disk_write_sectors(lba, buf, count);
  83. return ata_write_sectors(d->bus, d->unit, lba, buf, count);
  84. }