disk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* Disk layer: read/write sectors via direct FDC + ISA DMA in protected mode. */
  2. #include "disk.h"
  3. #include "io.h"
  4. #include "timer.h"
  5. #define SECTOR_SIZE 512
  6. #define SECTORS_PER_TRACK 18
  7. #define HEADS 2
  8. #define DMA_BUF_ADDR 0x4000 /* low RAM below the kernel; single sector (64 KB-safe) */
  9. /* FDC I/O ports */
  10. #define FDC_DOR 0x3F2
  11. #define FDC_MSR 0x3F4
  12. #define FDC_FIFO 0x3F5
  13. #define FDC_CCR 0x3F7
  14. /* ISA DMA channel 2 ports */
  15. #define DMA_CH2_ADDR 0x04
  16. #define DMA_CH2_COUNT 0x05
  17. #define DMA_CH2_PAGE 0x81
  18. #define DMA_SINGLEMASK 0x0A
  19. #define DMA_MODE 0x0B
  20. #define DMA_FLIPFLOP 0x0C
  21. /* MSR bits */
  22. #define MSR_RQM 0x80
  23. #define MSR_DIO 0x40
  24. #define MSR_CB 0x10
  25. /* FDC commands */
  26. #define CMD_SPECIFY 0x03
  27. #define CMD_RECALIBRATE 0x07
  28. #define CMD_SENSE_INT 0x08
  29. #define CMD_READ_DATA 0x66 /* READ | MFM | SK (no Multi-Track) */
  30. #define CMD_WRITE_DATA 0xC5 /* WRITE | MFM */
  31. #define CMD_SEEK 0x0F
  32. #define MOTOR_DELAY_MS 300
  33. #define IRQ_TIMEOUT_MS 2000
  34. #define PROBE_TIMEOUT_MS 500 /* boot-scan probe: shorter than normal I/O */
  35. #define MAX_RETRIES 10
  36. #define SEEK_SETTLE_MS 25 /* head settle after seek */
  37. /* PIC master mask port */
  38. #define PIC1_DATA 0x21
  39. static inline unsigned char inb(unsigned short port)
  40. {
  41. unsigned char v;
  42. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  43. return v;
  44. }
  45. static inline void outb(unsigned short port, unsigned char v)
  46. {
  47. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  48. }
  49. volatile int fdc_irq_fired;
  50. void fdc_irq(void)
  51. {
  52. fdc_irq_fired = 1;
  53. outb(0x20, 0x20);
  54. }
  55. static int fdc_initialized;
  56. static int fdc_absent; /* controller did not answer at all */
  57. static unsigned int motor_mask; /* bits 0/1: motor spinning for drive N */
  58. static unsigned int cur_cylinder;
  59. static unsigned int current_drive;
  60. static unsigned int irq_timeout_ms = IRQ_TIMEOUT_MS;
  61. static int quiet_errors; /* presence probe: don't print read errors */
  62. /* ---- low-level FDC helpers ---- */
  63. static int fdc_wait_rqm(void)
  64. {
  65. unsigned int t0 = get_ticks_ms();
  66. while ((get_ticks_ms() - t0) < 500) {
  67. if (inb(FDC_MSR) & MSR_RQM)
  68. return 0;
  69. }
  70. return -1;
  71. }
  72. static int fdc_send(unsigned char b)
  73. {
  74. if (fdc_wait_rqm() != 0)
  75. return -1;
  76. if (inb(FDC_MSR) & MSR_DIO)
  77. return -1;
  78. outb(FDC_FIFO, b);
  79. return 0;
  80. }
  81. static int fdc_recv(unsigned char *b)
  82. {
  83. if (fdc_wait_rqm() != 0)
  84. return -1;
  85. if (!(inb(FDC_MSR) & MSR_DIO))
  86. return -1;
  87. *b = inb(FDC_FIFO);
  88. return 0;
  89. }
  90. static int fdc_wait_irq(void)
  91. {
  92. unsigned int t0 = get_ticks_ms();
  93. while (!fdc_irq_fired) {
  94. if ((get_ticks_ms() - t0) >= irq_timeout_ms)
  95. return -1;
  96. __asm__ volatile ("hlt");
  97. }
  98. return 0;
  99. }
  100. /* A controller that reads back 0x00/0xFF on the MSR is not there (or the
  101. * floppy function is disabled). Checking this before waiting for IRQ 6 keeps
  102. * a machine with no floppy controller from sitting in a 2 s timeout. */
  103. static int fdc_controller_alive(void)
  104. {
  105. unsigned int t0 = get_ticks_ms();
  106. for (;;) {
  107. unsigned char msr = inb(FDC_MSR);
  108. if (msr != 0x00 && msr != 0xFF && (msr & MSR_RQM))
  109. return 1;
  110. if ((get_ticks_ms() - t0) >= 200)
  111. return 0;
  112. }
  113. }
  114. static int fdc_sense_interrupt(unsigned char *st0, unsigned char *cyl)
  115. {
  116. if (fdc_send(CMD_SENSE_INT) != 0)
  117. return -1;
  118. if (fdc_recv(st0) != 0)
  119. return -1;
  120. if (fdc_recv(cyl) != 0)
  121. return -1;
  122. return 0;
  123. }
  124. /* ---- motor control ---- */
  125. /* Program DOR: controller on, DMA+IRQ enabled, current drive selected, and
  126. * every motor that has been spun up left running. Keeping the other drive's
  127. * motor bit set avoids a second 300 ms spin-up when the scan switches from
  128. * floppy0 to floppy1. */
  129. static void fdc_select_drive(void)
  130. {
  131. unsigned int dor = 0x0Cu | current_drive;
  132. if (motor_mask & 1u)
  133. dor |= 1u << 4;
  134. if (motor_mask & 2u)
  135. dor |= 1u << 5;
  136. outb(FDC_DOR, (unsigned char)dor);
  137. }
  138. static void fdc_motor_on(void)
  139. {
  140. unsigned int bit = 1u << current_drive;
  141. if (motor_mask & bit) {
  142. fdc_select_drive(); /* switch select bits, no delay */
  143. return;
  144. }
  145. motor_mask |= bit;
  146. fdc_select_drive();
  147. {
  148. unsigned int t0 = get_ticks_ms();
  149. while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
  150. __asm__ volatile ("hlt");
  151. }
  152. }
  153. /* ---- reset ---- */
  154. static int fdc_reset(void)
  155. {
  156. unsigned char st0, cyl;
  157. int i;
  158. if (fdc_absent)
  159. return -1;
  160. /* Pull reset line: DOR = 0 turns off controller + motor */
  161. outb(FDC_DOR, 0x00);
  162. motor_mask = 0;
  163. {
  164. unsigned int t0 = get_ticks_ms();
  165. while ((get_ticks_ms() - t0) < 10)
  166. ;
  167. }
  168. /* Release reset: controller on, DMA/IRQ enabled, no motor.
  169. * The FDC fires IRQ 6 once the reset completes. */
  170. fdc_irq_fired = 0;
  171. fdc_select_drive();
  172. if (fdc_wait_irq() != 0)
  173. return -1;
  174. for (i = 0; i < 4; i++)
  175. fdc_sense_interrupt(&st0, &cyl);
  176. outb(FDC_CCR, 0x00); /* 500 kbps for 1.44 MB */
  177. /* SPECIFY: SRT=12ms, HUT=240ms, HLT=30ms, DMA mode
  178. * Conservative timings for real hardware compatibility.
  179. * Byte 1: SRT_nibble=4 (16-4=12ms), HUT_nibble=F (15*16=240ms) → 0x4F
  180. * Byte 2: HLT=15 (15*2=30ms)<<1 | NDMA=0 → 0x1E */
  181. if (fdc_send(CMD_SPECIFY) != 0)
  182. return -1;
  183. if (fdc_send(0x4F) != 0)
  184. return -1;
  185. if (fdc_send(0x1E) != 0)
  186. return -1;
  187. return 0;
  188. }
  189. static int fdc_recalibrate(void)
  190. {
  191. unsigned char st0, cyl;
  192. fdc_irq_fired = 0;
  193. if (fdc_send(CMD_RECALIBRATE) != 0)
  194. return -1;
  195. if (fdc_send((unsigned char)current_drive) != 0)
  196. return -1;
  197. if (fdc_wait_irq() != 0)
  198. return -1;
  199. if (fdc_sense_interrupt(&st0, &cyl) != 0)
  200. return -1;
  201. if (st0 & 0xC0)
  202. return -1;
  203. cur_cylinder = 0;
  204. return 0;
  205. }
  206. static int fdc_seek(unsigned int cylinder, unsigned int head)
  207. {
  208. unsigned char st0, cyl;
  209. fdc_irq_fired = 0;
  210. if (fdc_send(CMD_SEEK) != 0)
  211. return -1;
  212. if (fdc_send((unsigned char)((head << 2) | current_drive)) != 0)
  213. return -1;
  214. if (fdc_send((unsigned char)cylinder) != 0)
  215. return -1;
  216. if (fdc_wait_irq() != 0)
  217. return -1;
  218. if (fdc_sense_interrupt(&st0, &cyl) != 0)
  219. return -1;
  220. if ((st0 & 0xC0) || cyl != (unsigned char)cylinder)
  221. return -1;
  222. cur_cylinder = cylinder;
  223. /* Let head settle on real hardware before issuing read (avoids LBA 33+ read failures). */
  224. {
  225. unsigned int t0 = get_ticks_ms();
  226. while ((get_ticks_ms() - t0) < SEEK_SETTLE_MS)
  227. ;
  228. }
  229. return 0;
  230. }
  231. /* ---- DMA channel 2 setup ---- */
  232. static void dma_setup_read(unsigned int phys, unsigned int len)
  233. {
  234. unsigned int count = len - 1;
  235. outb(DMA_SINGLEMASK, 0x06); /* mask channel 2 */
  236. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  237. outb(DMA_CH2_ADDR, (unsigned char)(phys & 0xFF));
  238. outb(DMA_CH2_ADDR, (unsigned char)((phys >> 8) & 0xFF));
  239. outb(DMA_CH2_PAGE, (unsigned char)((phys >> 16) & 0xFF));
  240. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  241. outb(DMA_CH2_COUNT, (unsigned char)(count & 0xFF));
  242. outb(DMA_CH2_COUNT, (unsigned char)((count >> 8) & 0xFF));
  243. outb(DMA_MODE, 0x46); /* single, increment, write, ch2 */
  244. outb(DMA_SINGLEMASK, 0x02); /* unmask channel 2 */
  245. }
  246. static void dma_setup_write(unsigned int phys, unsigned int len)
  247. {
  248. unsigned int count = len - 1;
  249. outb(DMA_SINGLEMASK, 0x06); /* mask channel 2 */
  250. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  251. outb(DMA_CH2_ADDR, (unsigned char)(phys & 0xFF));
  252. outb(DMA_CH2_ADDR, (unsigned char)((phys >> 8) & 0xFF));
  253. outb(DMA_CH2_PAGE, (unsigned char)((phys >> 16) & 0xFF));
  254. outb(DMA_FLIPFLOP, 0xFF); /* reset flip-flop */
  255. outb(DMA_CH2_COUNT, (unsigned char)(count & 0xFF));
  256. outb(DMA_CH2_COUNT, (unsigned char)((count >> 8) & 0xFF));
  257. outb(DMA_MODE, 0x4A); /* single, increment, read (mem->dev), ch2 */
  258. outb(DMA_SINGLEMASK, 0x02); /* unmask channel 2 */
  259. }
  260. /* ---- read one sector ---- */
  261. static void lba_to_chs(unsigned int lba,
  262. unsigned int *cyl, unsigned int *head, unsigned int *sec)
  263. {
  264. unsigned int tmp = lba / SECTORS_PER_TRACK;
  265. *sec = (lba % SECTORS_PER_TRACK) + 1;
  266. *head = tmp % HEADS;
  267. *cyl = tmp / HEADS;
  268. }
  269. static int fdc_do_read(unsigned int cyl, unsigned int head, unsigned int sec)
  270. {
  271. unsigned char results[7];
  272. int i;
  273. dma_setup_read(DMA_BUF_ADDR, SECTOR_SIZE);
  274. fdc_irq_fired = 0;
  275. if (fdc_send(CMD_READ_DATA) != 0) return -1;
  276. if (fdc_send((unsigned char)((head << 2) | current_drive)) != 0) return -1;
  277. if (fdc_send((unsigned char)cyl) != 0) return -1;
  278. if (fdc_send((unsigned char)head) != 0) return -1;
  279. if (fdc_send((unsigned char)sec) != 0) return -1;
  280. if (fdc_send(0x02) != 0) return -1; /* 512 bytes/sector */
  281. if (fdc_send((unsigned char)SECTORS_PER_TRACK) != 0) return -1;
  282. if (fdc_send(0x1B) != 0) return -1; /* gap3 */
  283. if (fdc_send(0xFF) != 0) return -1; /* data length */
  284. if (fdc_wait_irq() != 0)
  285. return -2;
  286. for (i = 0; i < 7; i++) {
  287. if (fdc_recv(&results[i]) != 0)
  288. return -3;
  289. }
  290. if (results[0] & 0xC0) {
  291. if (!quiet_errors) {
  292. io_putstr("fdc: CHS=");
  293. io_puthex(cyl);
  294. io_putchar('/');
  295. io_puthex(head);
  296. io_putchar('/');
  297. io_puthex(sec);
  298. io_putstr(" ST0=");
  299. io_puthex(results[0]);
  300. io_putstr(" ST1=");
  301. io_puthex(results[1]);
  302. io_putstr(" ST2=");
  303. io_puthex(results[2]);
  304. io_putchar('\n');
  305. }
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. static int fdc_do_write(unsigned int cyl, unsigned int head, unsigned int sec)
  311. {
  312. unsigned char results[7];
  313. int i;
  314. dma_setup_write(DMA_BUF_ADDR, SECTOR_SIZE);
  315. fdc_irq_fired = 0;
  316. if (fdc_send(CMD_WRITE_DATA) != 0) return -1;
  317. if (fdc_send((unsigned char)((head << 2) | current_drive)) != 0) return -1;
  318. if (fdc_send((unsigned char)cyl) != 0) return -1;
  319. if (fdc_send((unsigned char)head) != 0) return -1;
  320. if (fdc_send((unsigned char)sec) != 0) return -1;
  321. if (fdc_send(0x02) != 0) return -1; /* 512 bytes/sector */
  322. if (fdc_send((unsigned char)SECTORS_PER_TRACK) != 0) return -1;
  323. if (fdc_send(0x1B) != 0) return -1; /* gap3 */
  324. if (fdc_send(0xFF) != 0) return -1; /* data length */
  325. if (fdc_wait_irq() != 0)
  326. return -2;
  327. for (i = 0; i < 7; i++) {
  328. if (fdc_recv(&results[i]) != 0)
  329. return -3;
  330. }
  331. if (results[0] & 0xC0) {
  332. if (!quiet_errors) {
  333. io_putstr("fdc write: CHS=");
  334. io_puthex(cyl);
  335. io_putchar('/');
  336. io_puthex(head);
  337. io_putchar('/');
  338. io_puthex(sec);
  339. io_putstr(" ST0=");
  340. io_puthex(results[0]);
  341. io_putstr(" ST1=");
  342. io_puthex(results[1]);
  343. io_putstr(" ST2=");
  344. io_puthex(results[2]);
  345. io_putchar('\n');
  346. }
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. static int fdc_read_one(unsigned int lba)
  352. {
  353. unsigned int cyl, head, sec;
  354. lba_to_chs(lba, &cyl, &head, &sec);
  355. if (cyl != cur_cylinder) {
  356. if (fdc_seek(cyl, head) != 0)
  357. return -1;
  358. }
  359. return fdc_do_read(cyl, head, sec);
  360. }
  361. static int fdc_write_one(unsigned int lba)
  362. {
  363. unsigned int cyl, head, sec;
  364. lba_to_chs(lba, &cyl, &head, &sec);
  365. if (cyl != cur_cylinder) {
  366. if (fdc_seek(cyl, head) != 0)
  367. return -1;
  368. }
  369. return fdc_do_write(cyl, head, sec);
  370. }
  371. /* Aggressive retry: seek far away then back to improve head alignment. */
  372. static int fdc_read_one_retry(unsigned int lba)
  373. {
  374. unsigned int cyl, head, sec;
  375. lba_to_chs(lba, &cyl, &head, &sec);
  376. /* Seek to cylinder 40 (mid-disk) first, then to target.
  377. * The long travel forces the stepper to settle accurately. */
  378. fdc_seek(cyl > 40 ? 0 : 40, 0);
  379. if (fdc_seek(cyl, head) != 0)
  380. return -1;
  381. return fdc_do_read(cyl, head, sec);
  382. }
  383. static int fdc_write_one_retry(unsigned int lba)
  384. {
  385. unsigned int cyl, head, sec;
  386. lba_to_chs(lba, &cyl, &head, &sec);
  387. fdc_seek(cyl > 40 ? 0 : 40, 0);
  388. if (fdc_seek(cyl, head) != 0)
  389. return -1;
  390. return fdc_do_write(cyl, head, sec);
  391. }
  392. /* ---- public API ---- */
  393. static int fdc_init_once(void)
  394. {
  395. if (fdc_absent)
  396. return -1;
  397. /* Mask IRQ 6 before reset so the completion interrupt doesn't
  398. * fire before we're ready (IRQ 6 may already be unmasked from
  399. * a previous init). Then clear the flag and release the reset. */
  400. outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
  401. outb(FDC_DOR, 0x00);
  402. motor_mask = 0;
  403. {
  404. unsigned int t0 = get_ticks_ms();
  405. while ((get_ticks_ms() - t0) < 10)
  406. ;
  407. }
  408. fdc_irq_fired = 0;
  409. fdc_select_drive();
  410. /* Unmask IRQ 6 — the latched reset-completion interrupt fires now. */
  411. outb(PIC1_DATA, inb(PIC1_DATA) & ~0x40);
  412. /* No FDC at all: fail now instead of waiting out the IRQ timeout. */
  413. if (!fdc_controller_alive()) {
  414. outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
  415. fdc_absent = 1;
  416. return -1;
  417. }
  418. if (fdc_wait_irq() != 0)
  419. return -1;
  420. {
  421. unsigned char st0, cyl;
  422. int i;
  423. for (i = 0; i < 4; i++)
  424. fdc_sense_interrupt(&st0, &cyl);
  425. outb(FDC_CCR, 0x00);
  426. if (fdc_send(CMD_SPECIFY) != 0)
  427. return -1;
  428. if (fdc_send(0x4F) != 0)
  429. return -1;
  430. if (fdc_send(0x1E) != 0)
  431. return -1;
  432. }
  433. fdc_motor_on();
  434. if (fdc_recalibrate() != 0)
  435. return -1;
  436. fdc_initialized = 1;
  437. return 0;
  438. }
  439. void disk_set_drive(unsigned int drive)
  440. {
  441. if (drive > 1)
  442. return;
  443. if (drive == current_drive)
  444. return;
  445. current_drive = drive;
  446. if (fdc_initialized) {
  447. /* Same controller, different drive: its motor may still be off
  448. * and its head position is unknown. Keep the controller
  449. * initialised instead of paying a full reset. */
  450. cur_cylinder = 0xFFFFFFFFu;
  451. fdc_motor_on();
  452. fdc_recalibrate();
  453. }
  454. }
  455. /* Quick presence/media probe used by the boot scan. One command with a short
  456. * timeout, never the ten-retry recovery loop: an empty drive must not hold the
  457. * UI back. Normal I/O after a successful mount still gets full retries. */
  458. int disk_probe(unsigned int drive)
  459. {
  460. int rc;
  461. if (drive > 1 || fdc_absent)
  462. return 0;
  463. irq_timeout_ms = PROBE_TIMEOUT_MS;
  464. disk_set_drive(drive);
  465. if (!fdc_initialized && fdc_init_once() != 0) {
  466. irq_timeout_ms = IRQ_TIMEOUT_MS;
  467. return 0;
  468. }
  469. quiet_errors = 1;
  470. rc = fdc_read_one(0);
  471. quiet_errors = 0;
  472. irq_timeout_ms = IRQ_TIMEOUT_MS;
  473. if (rc != 0) {
  474. /* A timed-out command may still be sitting in the FDC: force a
  475. * clean re-init next time. A normal error response (empty
  476. * drive / no media) leaves the controller usable, so keep it
  477. * initialized for the mount that follows. */
  478. if (rc == -2)
  479. fdc_initialized = 0;
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. unsigned int disk_get_drive(void)
  485. {
  486. return current_drive;
  487. }
  488. int disk_read_sectors(unsigned int lba, void *buf, unsigned int count)
  489. {
  490. unsigned int i;
  491. if (!fdc_initialized) {
  492. if (fdc_init_once() != 0)
  493. return -1;
  494. }
  495. for (i = 0; i < count; i++) {
  496. int attempt, ok = 0;
  497. for (attempt = 0; attempt < MAX_RETRIES; attempt++) {
  498. int rc;
  499. if (attempt < 3) {
  500. rc = fdc_read_one(lba + i);
  501. } else {
  502. /* Long-seek retry: seek far away then back to
  503. * force accurate head positioning. */
  504. rc = fdc_read_one_retry(lba + i);
  505. }
  506. if (rc == 0) {
  507. ok = 1;
  508. break;
  509. }
  510. fdc_reset();
  511. fdc_motor_on();
  512. fdc_recalibrate();
  513. }
  514. if (!ok) {
  515. io_putstr("disk: read failed LBA=");
  516. io_puthex(lba + i);
  517. io_putstr(" (sector ");
  518. io_puthex(i);
  519. io_putstr(" of ");
  520. io_puthex(count);
  521. io_putstr(")\n");
  522. return -1;
  523. }
  524. {
  525. unsigned int j;
  526. unsigned char *dst = (unsigned char *)buf + (i * SECTOR_SIZE);
  527. const unsigned char *src = (const unsigned char *)DMA_BUF_ADDR;
  528. for (j = 0; j < SECTOR_SIZE; j++)
  529. dst[j] = src[j];
  530. }
  531. }
  532. return 0;
  533. }
  534. int disk_write_sectors(unsigned int lba, const void *buf, unsigned int count)
  535. {
  536. unsigned int i;
  537. if (!fdc_initialized) {
  538. if (fdc_init_once() != 0)
  539. return -1;
  540. }
  541. for (i = 0; i < count; i++) {
  542. int attempt, ok = 0;
  543. /* Copy the sector to write into the ISA DMA buffer first. */
  544. {
  545. unsigned int j;
  546. const unsigned char *src = (const unsigned char *)buf + (i * SECTOR_SIZE);
  547. volatile unsigned char *dst = (volatile unsigned char *)DMA_BUF_ADDR;
  548. for (j = 0; j < SECTOR_SIZE; j++)
  549. dst[j] = src[j];
  550. }
  551. for (attempt = 0; attempt < MAX_RETRIES; attempt++) {
  552. int rc;
  553. if (attempt < 3) {
  554. rc = fdc_write_one(lba + i);
  555. } else {
  556. rc = fdc_write_one_retry(lba + i);
  557. }
  558. if (rc == 0) {
  559. ok = 1;
  560. break;
  561. }
  562. fdc_reset();
  563. fdc_motor_on();
  564. fdc_recalibrate();
  565. }
  566. if (!ok) {
  567. io_putstr("disk: write failed LBA=");
  568. io_puthex(lba + i);
  569. io_putstr(" (sector ");
  570. io_puthex(i);
  571. io_putstr(" of ");
  572. io_puthex(count);
  573. io_putstr(")\n");
  574. return -1;
  575. }
  576. }
  577. return 0;
  578. }