disk.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <assert.h>
  33. #include <stdint.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #include <err.h>
  38. #include "support.h"
  39. #include "disk.h"
  40. #define HOSTSTRING "System/161 Disk Image"
  41. #define BLOCKSIZE 512
  42. #ifndef EINTR
  43. #define EINTR 0
  44. #endif
  45. static int fd=-1;
  46. static uint32_t nblocks;
  47. void
  48. opendisk(const char *path)
  49. {
  50. struct stat statbuf;
  51. assert(fd<0);
  52. fd = open(path, O_RDWR);
  53. if (fd<0) {
  54. err(1, "%s", path);
  55. }
  56. if (fstat(fd, &statbuf)) {
  57. err(1, "%s: fstat", path);
  58. }
  59. nblocks = statbuf.st_size / BLOCKSIZE;
  60. #ifdef HOST
  61. nblocks--;
  62. {
  63. char buf[64];
  64. int len;
  65. do {
  66. len = read(fd, buf, sizeof(buf)-1);
  67. if (len < 0 && (errno==EINTR || errno==EAGAIN)) {
  68. continue;
  69. }
  70. } while (0);
  71. buf[len] = 0;
  72. buf[strlen(HOSTSTRING)] = 0;
  73. if (strcmp(buf, HOSTSTRING)) {
  74. errx(1, "%s: Not a System/161 disk image", path);
  75. }
  76. }
  77. #endif
  78. }
  79. uint32_t
  80. diskblocksize(void)
  81. {
  82. assert(fd>=0);
  83. return BLOCKSIZE;
  84. }
  85. uint32_t
  86. diskblocks(void)
  87. {
  88. assert(fd>=0);
  89. return nblocks;
  90. }
  91. void
  92. diskwrite(const void *data, uint32_t block)
  93. {
  94. const char *cdata = data;
  95. uint32_t tot=0;
  96. int len;
  97. assert(fd>=0);
  98. #ifdef HOST
  99. // skip over disk file header
  100. block++;
  101. #endif
  102. if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
  103. err(1, "lseek");
  104. }
  105. while (tot < BLOCKSIZE) {
  106. len = write(fd, cdata + tot, BLOCKSIZE - tot);
  107. if (len < 0) {
  108. if (errno==EINTR || errno==EAGAIN) {
  109. continue;
  110. }
  111. err(1, "write");
  112. }
  113. if (len==0) {
  114. err(1, "write returned 0?");
  115. }
  116. tot += len;
  117. }
  118. }
  119. void
  120. diskread(void *data, uint32_t block)
  121. {
  122. char *cdata = data;
  123. uint32_t tot=0;
  124. int len;
  125. assert(fd>=0);
  126. #ifdef HOST
  127. // skip over disk file header
  128. block++;
  129. #endif
  130. if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
  131. err(1, "lseek");
  132. }
  133. while (tot < BLOCKSIZE) {
  134. len = read(fd, cdata + tot, BLOCKSIZE - tot);
  135. if (len < 0) {
  136. if (errno==EINTR || errno==EAGAIN) {
  137. continue;
  138. }
  139. err(1, "read");
  140. }
  141. if (len==0) {
  142. err(1, "unexpected EOF in mid-sector");
  143. }
  144. tot += len;
  145. }
  146. }
  147. void
  148. closedisk(void)
  149. {
  150. assert(fd>=0);
  151. if (close(fd)) {
  152. err(1, "close");
  153. }
  154. fd = -1;
  155. }