disk.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <disk.h>
  6. static Disk*
  7. mkwidth(Disk *disk)
  8. {
  9. char buf[40];
  10. sprint(buf, "%lld", disk->size);
  11. disk->width = strlen(buf);
  12. return disk;
  13. }
  14. /*
  15. * Discover the disk geometry by various sleazeful means.
  16. *
  17. * First, if there is a partition table in sector 0,
  18. * see if all the partitions have the same end head
  19. * and sector; if so, we'll assume that that's the
  20. * right count.
  21. *
  22. * If that fails, we'll try looking at the geometry that the ATA
  23. * driver supplied, if any, and translate that as a
  24. * BIOS might.
  25. *
  26. * If that too fails, which should only happen on a SCSI
  27. * disk with no currently defined partitions, we'll try
  28. * various common (h, s) pairs used by BIOSes when faking
  29. * the geometries.
  30. */
  31. typedef struct Table Table;
  32. typedef struct Tentry Tentry;
  33. struct Tentry {
  34. uchar active; /* active flag */
  35. uchar starth; /* starting head */
  36. uchar starts; /* starting sector */
  37. uchar startc; /* starting cylinder */
  38. uchar type; /* partition type */
  39. uchar endh; /* ending head */
  40. uchar ends; /* ending sector */
  41. uchar endc; /* ending cylinder */
  42. uchar xlba[4]; /* starting LBA from beginning of disc */
  43. uchar xsize[4]; /* size in sectors */
  44. };
  45. enum {
  46. Toffset = 446, /* offset of partition table in sector */
  47. Magic0 = 0x55,
  48. Magic1 = 0xAA,
  49. NTentry = 4,
  50. };
  51. struct Table {
  52. Tentry entry[NTentry];
  53. uchar magic[2];
  54. };
  55. static int
  56. partitiongeometry(Disk *disk)
  57. {
  58. char *rawname;
  59. int i, h, rawfd, s;
  60. uchar buf[512];
  61. Table *t;
  62. t = (Table*)(buf + Toffset);
  63. /*
  64. * look for an MBR first in the /dev/sdXX/data partition, otherwise
  65. * attempt to fall back on the current partition.
  66. */
  67. rawname = malloc(strlen(disk->prefix) + 5); /* prefix + "data" + nul */
  68. if(rawname == nil)
  69. return -1;
  70. strcpy(rawname, disk->prefix);
  71. strcat(rawname, "data");
  72. rawfd = open(rawname, OREAD);
  73. free(rawname);
  74. if(rawfd >= 0
  75. && seek(rawfd, 0, 0) >= 0
  76. && readn(rawfd, buf, 512) == 512
  77. && t->magic[0] == Magic0
  78. && t->magic[1] == Magic1) {
  79. close(rawfd);
  80. } else {
  81. if(rawfd >= 0)
  82. close(rawfd);
  83. if(seek(disk->fd, 0, 0) < 0
  84. || readn(disk->fd, buf, 512) != 512
  85. || t->magic[0] != Magic0
  86. || t->magic[1] != Magic1) {
  87. return -1;
  88. }
  89. }
  90. h = s = -1;
  91. for(i=0; i<NTentry; i++) {
  92. if(t->entry[i].type == 0)
  93. continue;
  94. t->entry[i].ends &= 63;
  95. if(h == -1) {
  96. h = t->entry[i].endh;
  97. s = t->entry[i].ends;
  98. } else {
  99. /*
  100. * Only accept the partition info if every
  101. * partition is consistent.
  102. */
  103. if(h != t->entry[i].endh || s != t->entry[i].ends)
  104. return -1;
  105. }
  106. }
  107. if(h == -1)
  108. return -1;
  109. disk->h = h+1; /* heads count from 0 */
  110. disk->s = s; /* sectors count from 1 */
  111. disk->c = disk->secs / (disk->h*disk->s);
  112. disk->chssrc = Gpart;
  113. return 0;
  114. }
  115. /*
  116. * If there is ATA geometry, use it, perhaps massaged.
  117. */
  118. static int
  119. drivergeometry(Disk *disk)
  120. {
  121. int m;
  122. if(disk->c == 0 || disk->h == 0 || disk->s == 0)
  123. return -1;
  124. disk->chssrc = Gdisk;
  125. if(disk->c < 1024)
  126. return 0;
  127. switch(disk->h) {
  128. case 15:
  129. disk->h = 255;
  130. disk->c /= 17;
  131. return 0;
  132. default:
  133. for(m = 2; m*disk->h < 256; m *= 2) {
  134. if(disk->c/m < 1024) {
  135. disk->c /= m;
  136. disk->h *= m;
  137. return 0;
  138. }
  139. }
  140. /* set to 255, 63 and be done with it */
  141. disk->h = 255;
  142. disk->s = 63;
  143. disk->c = disk->secs / (disk->h * disk->s);
  144. return 0;
  145. }
  146. }
  147. /*
  148. * There's no ATA geometry and no partitions.
  149. * Our guess is as good as anyone's.
  150. */
  151. static struct {
  152. int h;
  153. int s;
  154. } guess[] = {
  155. 64, 32,
  156. 64, 63,
  157. 128, 63,
  158. 255, 63,
  159. };
  160. static int
  161. guessgeometry(Disk *disk)
  162. {
  163. int i;
  164. long c;
  165. disk->chssrc = Gguess;
  166. c = 1024;
  167. for(i=0; i<nelem(guess); i++)
  168. if(c*guess[i].h*guess[i].s >= disk->secs) {
  169. disk->h = guess[i].h;
  170. disk->s = guess[i].s;
  171. disk->c = disk->secs / (disk->h * disk->s);
  172. return 0;
  173. }
  174. /* use maximum values */
  175. disk->h = 255;
  176. disk->s = 63;
  177. disk->c = disk->secs / (disk->h * disk->s);
  178. return 0;
  179. }
  180. static void
  181. findgeometry(Disk *disk)
  182. {
  183. if(partitiongeometry(disk) < 0
  184. && drivergeometry(disk) < 0
  185. && guessgeometry(disk) < 0) { /* can't happen */
  186. print("we're completely confused about your disk; sorry\n");
  187. assert(0);
  188. }
  189. }
  190. static Disk*
  191. openfile(Disk *disk)
  192. {
  193. Dir *d;
  194. if((d = dirfstat(disk->fd)) == nil){
  195. free(disk);
  196. return nil;
  197. }
  198. disk->secsize = 512;
  199. disk->size = d->length;
  200. disk->secs = disk->size / disk->secsize;
  201. disk->offset = 0;
  202. free(d);
  203. findgeometry(disk);
  204. return mkwidth(disk);
  205. }
  206. static Disk*
  207. opensd(Disk *disk)
  208. {
  209. Biobuf b;
  210. char *p, *f[10];
  211. int nf;
  212. Binit(&b, disk->ctlfd, OREAD);
  213. while(p = Brdline(&b, '\n')) {
  214. p[Blinelen(&b)-1] = '\0';
  215. nf = tokenize(p, f, nelem(f));
  216. if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
  217. disk->secsize = strtoll(f[2], 0, 0);
  218. if(nf >= 6) {
  219. disk->c = strtol(f[3], 0, 0);
  220. disk->h = strtol(f[4], 0, 0);
  221. disk->s = strtol(f[5], 0, 0);
  222. }
  223. }
  224. if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
  225. disk->offset = strtoll(f[2], 0, 0);
  226. disk->secs = strtoll(f[3], 0, 0) - disk->offset;
  227. }
  228. }
  229. disk->size = disk->secs * disk->secsize;
  230. if(disk->size <= 0) {
  231. strcpy(disk->part, "");
  232. disk->type = Tfile;
  233. return openfile(disk);
  234. }
  235. findgeometry(disk);
  236. return mkwidth(disk);
  237. }
  238. Disk*
  239. opendisk(char *disk, int rdonly, int noctl)
  240. {
  241. char *p, *q;
  242. Disk *d;
  243. d = mallocz(sizeof(*d), 1);
  244. if(d == nil)
  245. return nil;
  246. d->fd = d->wfd = d->ctlfd = -1;
  247. d->rdonly = rdonly;
  248. d->fd = open(disk, OREAD);
  249. if(d->fd < 0) {
  250. werrstr("cannot open disk file");
  251. free(d);
  252. return nil;
  253. }
  254. if(rdonly == 0) {
  255. d->wfd = open(disk, OWRITE);
  256. if(d->wfd < 0)
  257. d->rdonly = 1;
  258. }
  259. if(noctl)
  260. return openfile(d);
  261. p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
  262. if(p == nil) {
  263. close(d->wfd);
  264. close(d->fd);
  265. free(d);
  266. return nil;
  267. }
  268. strcpy(p, disk);
  269. /* check for floppy(3) disk */
  270. if(strlen(p) >= 7) {
  271. q = p+strlen(p)-7;
  272. if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
  273. strcpy(q+3, "ctl");
  274. if((d->ctlfd = open(p, ORDWR)) >= 0) {
  275. *q = '\0';
  276. d->prefix = p;
  277. d->type = Tfloppy;
  278. return openfile(d);
  279. }
  280. }
  281. }
  282. /* attempt to find sd(3) disk or partition */
  283. if(q = strrchr(p, '/'))
  284. q++;
  285. else
  286. q = p;
  287. strcpy(q, "ctl");
  288. if((d->ctlfd = open(p, ORDWR)) >= 0) {
  289. *q = '\0';
  290. d->prefix = p;
  291. d->type = Tsd;
  292. d->part = strdup(disk+(q-p));
  293. if(d->part == nil){
  294. close(d->ctlfd);
  295. close(d->wfd);
  296. close(d->fd);
  297. free(p);
  298. free(d);
  299. return nil;
  300. }
  301. return opensd(d);
  302. }
  303. *q = '\0';
  304. d->prefix = p;
  305. /* assume we just have a normal file */
  306. d->type = Tfile;
  307. return openfile(d);
  308. }