disk.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. return -1; /* not reached */
  147. }
  148. /*
  149. * There's no ATA geometry and no partitions.
  150. * Our guess is as good as anyone's.
  151. */
  152. static struct {
  153. int h;
  154. int s;
  155. } guess[] = {
  156. 64, 32,
  157. 64, 63,
  158. 128, 63,
  159. 255, 63,
  160. };
  161. static int
  162. guessgeometry(Disk *disk)
  163. {
  164. int i;
  165. long c;
  166. disk->chssrc = Gguess;
  167. c = 1024;
  168. for(i=0; i<nelem(guess); i++)
  169. if(c*guess[i].h*guess[i].s >= disk->secs) {
  170. disk->h = guess[i].h;
  171. disk->s = guess[i].s;
  172. disk->c = disk->secs / (disk->h * disk->s);
  173. return 0;
  174. }
  175. /* use maximum values */
  176. disk->h = 255;
  177. disk->s = 63;
  178. disk->c = disk->secs / (disk->h * disk->s);
  179. return 0;
  180. }
  181. static void
  182. findgeometry(Disk *disk)
  183. {
  184. if(partitiongeometry(disk) < 0
  185. && drivergeometry(disk) < 0
  186. && guessgeometry(disk) < 0) { /* can't happen */
  187. print("we're completely confused about your disk; sorry\n");
  188. assert(0);
  189. }
  190. }
  191. static Disk*
  192. openfile(Disk *disk)
  193. {
  194. Dir *d;
  195. if((d = dirfstat(disk->fd)) == nil){
  196. free(disk);
  197. return nil;
  198. }
  199. disk->secsize = 512;
  200. disk->size = d->length;
  201. disk->secs = disk->size / disk->secsize;
  202. disk->offset = 0;
  203. free(d);
  204. findgeometry(disk);
  205. return mkwidth(disk);
  206. }
  207. static Disk*
  208. opensd(Disk *disk)
  209. {
  210. Biobuf b;
  211. char *p, *f[10];
  212. int nf;
  213. Binit(&b, disk->ctlfd, OREAD);
  214. while(p = Brdline(&b, '\n')) {
  215. p[Blinelen(&b)-1] = '\0';
  216. nf = tokenize(p, f, nelem(f));
  217. if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
  218. disk->secsize = strtoll(f[2], 0, 0);
  219. if(nf >= 6) {
  220. disk->c = strtol(f[3], 0, 0);
  221. disk->h = strtol(f[4], 0, 0);
  222. disk->s = strtol(f[5], 0, 0);
  223. }
  224. }
  225. if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
  226. disk->offset = strtoll(f[2], 0, 0);
  227. disk->secs = strtoll(f[3], 0, 0) - disk->offset;
  228. }
  229. }
  230. disk->size = disk->secs * disk->secsize;
  231. if(disk->size <= 0) {
  232. strcpy(disk->part, "");
  233. disk->type = Tfile;
  234. return openfile(disk);
  235. }
  236. findgeometry(disk);
  237. return mkwidth(disk);
  238. }
  239. Disk*
  240. opendisk(char *disk, int rdonly, int noctl)
  241. {
  242. char *p, *q;
  243. Disk *d;
  244. d = mallocz(sizeof(*d), 1);
  245. if(d == nil)
  246. return nil;
  247. d->fd = d->wfd = d->ctlfd = -1;
  248. d->rdonly = rdonly;
  249. d->fd = open(disk, OREAD);
  250. if(d->fd < 0) {
  251. werrstr("cannot open disk file");
  252. free(d);
  253. return nil;
  254. }
  255. if(rdonly == 0) {
  256. d->wfd = open(disk, OWRITE);
  257. if(d->wfd < 0)
  258. d->rdonly = 1;
  259. }
  260. if(noctl)
  261. return openfile(d);
  262. p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
  263. if(p == nil) {
  264. close(d->wfd);
  265. close(d->fd);
  266. free(d);
  267. return nil;
  268. }
  269. strcpy(p, disk);
  270. /* check for floppy(3) disk */
  271. if(strlen(p) >= 7) {
  272. q = p+strlen(p)-7;
  273. if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
  274. strcpy(q+3, "ctl");
  275. if((d->ctlfd = open(p, ORDWR)) >= 0) {
  276. *q = '\0';
  277. d->prefix = p;
  278. d->type = Tfloppy;
  279. return openfile(d);
  280. }
  281. }
  282. }
  283. /* attempt to find sd(3) disk or partition */
  284. if(q = strrchr(p, '/'))
  285. q++;
  286. else
  287. q = p;
  288. strcpy(q, "ctl");
  289. if((d->ctlfd = open(p, ORDWR)) >= 0) {
  290. *q = '\0';
  291. d->prefix = p;
  292. d->type = Tsd;
  293. d->part = strdup(disk+(q-p));
  294. if(d->part == nil){
  295. close(d->ctlfd);
  296. close(d->wfd);
  297. close(d->fd);
  298. free(p);
  299. free(d);
  300. return nil;
  301. }
  302. return opensd(d);
  303. }
  304. *q = '\0';
  305. d->prefix = p;
  306. /* assume we just have a normal file */
  307. d->type = Tfile;
  308. return openfile(d);
  309. }