fdisk_sun.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * fdisk_sun.c
  3. *
  4. * I think this is mostly, or entirely, due to
  5. * Jakub Jelinek (jj@sunsite.mff.cuni.cz), July 1996
  6. *
  7. * Merged with fdisk for other architectures, aeb, June 1998.
  8. *
  9. * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  10. * Internationalization
  11. *
  12. * Licensed under GPLv2, see file LICENSE in this source tree.
  13. */
  14. #if ENABLE_FEATURE_SUN_LABEL
  15. #define SUNOS_SWAP 3
  16. #define SUN_WHOLE_DISK 5
  17. #define SUN_LABEL_MAGIC 0xDABE
  18. #define SUN_LABEL_MAGIC_SWAPPED 0xBEDA
  19. #define SUN_SSWAP16(x) (sun_other_endian ? fdisk_swap16(x) : (uint16_t)(x))
  20. #define SUN_SSWAP32(x) (sun_other_endian ? fdisk_swap32(x) : (uint32_t)(x))
  21. /* Copied from linux/major.h */
  22. #define FLOPPY_MAJOR 2
  23. #define SCSI_IOCTL_GET_IDLUN 0x5382
  24. static smallint sun_other_endian;
  25. static smallint scsi_disk;
  26. static smallint floppy;
  27. #ifndef IDE0_MAJOR
  28. #define IDE0_MAJOR 3
  29. #endif
  30. #ifndef IDE1_MAJOR
  31. #define IDE1_MAJOR 22
  32. #endif
  33. static void
  34. guess_device_type(void)
  35. {
  36. struct stat bootstat;
  37. if (fstat(dev_fd, &bootstat) < 0) {
  38. scsi_disk = 0;
  39. floppy = 0;
  40. } else if (S_ISBLK(bootstat.st_mode)
  41. && (major(bootstat.st_rdev) == IDE0_MAJOR ||
  42. major(bootstat.st_rdev) == IDE1_MAJOR)) {
  43. scsi_disk = 0;
  44. floppy = 0;
  45. } else if (S_ISBLK(bootstat.st_mode)
  46. && major(bootstat.st_rdev) == FLOPPY_MAJOR) {
  47. scsi_disk = 0;
  48. floppy = 1;
  49. } else {
  50. scsi_disk = 1;
  51. floppy = 0;
  52. }
  53. }
  54. static const char *const sun_sys_types[] ALIGN_PTR = {
  55. "\x00" "Empty" , /* 0 */
  56. "\x01" "Boot" , /* 1 */
  57. "\x02" "SunOS root" , /* 2 */
  58. "\x03" "SunOS swap" , /* SUNOS_SWAP */
  59. "\x04" "SunOS usr" , /* 4 */
  60. "\x05" "Whole disk" , /* SUN_WHOLE_DISK */
  61. "\x06" "SunOS stand" , /* 6 */
  62. "\x07" "SunOS var" , /* 7 */
  63. "\x08" "SunOS home" , /* 8 */
  64. "\x82" "Linux swap" , /* LINUX_SWAP */
  65. "\x83" "Linux native", /* LINUX_NATIVE */
  66. "\x8e" "Linux LVM" , /* 0x8e */
  67. /* New (2.2.x) raid partition with autodetect using persistent superblock */
  68. "\xfd" "Linux raid autodetect", /* 0xfd */
  69. NULL
  70. };
  71. static void
  72. set_sun_partition(int i, unsigned start, unsigned stop, int sysid)
  73. {
  74. sunlabel->infos[i].id = sysid;
  75. sunlabel->partitions[i].start_cylinder =
  76. SUN_SSWAP32(start / (g_heads * g_sectors));
  77. sunlabel->partitions[i].num_sectors =
  78. SUN_SSWAP32(stop - start);
  79. set_changed(i);
  80. }
  81. static int
  82. check_sun_label(void)
  83. {
  84. unsigned short *ush;
  85. int csum;
  86. if (sunlabel->magic != SUN_LABEL_MAGIC
  87. && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED
  88. ) {
  89. current_label_type = LABEL_DOS;
  90. sun_other_endian = 0;
  91. return 0;
  92. }
  93. sun_other_endian = (sunlabel->magic == SUN_LABEL_MAGIC_SWAPPED);
  94. ush = ((unsigned short *) (sunlabel + 1)) - 1;
  95. for (csum = 0; ush >= (unsigned short *)sunlabel;) csum ^= *ush--;
  96. if (csum) {
  97. printf("Detected sun disklabel with wrong checksum.\n"
  98. "Probably you'll have to set all the values,\n"
  99. "e.g. heads, sectors, cylinders and partitions\n"
  100. "or force a fresh label (s command in main menu)\n");
  101. } else {
  102. g_heads = SUN_SSWAP16(sunlabel->ntrks);
  103. g_cylinders = SUN_SSWAP16(sunlabel->ncyl);
  104. g_sectors = SUN_SSWAP16(sunlabel->nsect);
  105. }
  106. update_units();
  107. current_label_type = LABEL_SUN;
  108. g_partitions = 8;
  109. return 1;
  110. }
  111. static const struct sun_predefined_drives {
  112. const char *vendor;
  113. const char *model;
  114. unsigned short sparecyl;
  115. unsigned short ncyl;
  116. unsigned short nacyl;
  117. unsigned short pcylcount;
  118. unsigned short ntrks;
  119. unsigned short nsect;
  120. unsigned short rspeed;
  121. } sun_drives[] ALIGN_PTR = {
  122. { "Quantum","ProDrive 80S",1,832,2,834,6,34,3662},
  123. { "Quantum","ProDrive 105S",1,974,2,1019,6,35,3662},
  124. { "CDC","Wren IV 94171-344",3,1545,2,1549,9,46,3600},
  125. { "IBM","DPES-31080",0,4901,2,4903,4,108,5400},
  126. { "IBM","DORS-32160",0,1015,2,1017,67,62,5400},
  127. { "IBM","DNES-318350",0,11199,2,11474,10,320,7200},
  128. { "SEAGATE","ST34371",0,3880,2,3882,16,135,7228},
  129. { "","SUN0104",1,974,2,1019,6,35,3662},
  130. { "","SUN0207",4,1254,2,1272,9,36,3600},
  131. { "","SUN0327",3,1545,2,1549,9,46,3600},
  132. { "","SUN0340",0,1538,2,1544,6,72,4200},
  133. { "","SUN0424",2,1151,2,2500,9,80,4400},
  134. { "","SUN0535",0,1866,2,2500,7,80,5400},
  135. { "","SUN0669",5,1614,2,1632,15,54,3600},
  136. { "","SUN1.0G",5,1703,2,1931,15,80,3597},
  137. { "","SUN1.05",0,2036,2,2038,14,72,5400},
  138. { "","SUN1.3G",6,1965,2,3500,17,80,5400},
  139. { "","SUN2.1G",0,2733,2,3500,19,80,5400},
  140. { "IOMEGA","Jaz",0,1019,2,1021,64,32,5394},
  141. };
  142. static const struct sun_predefined_drives *
  143. sun_autoconfigure_scsi(void)
  144. {
  145. const struct sun_predefined_drives *p = NULL;
  146. #ifdef SCSI_IOCTL_GET_IDLUN
  147. unsigned int id[2];
  148. char buffer[2048];
  149. char buffer2[2048];
  150. FILE *pfd;
  151. char *vendor;
  152. char *model;
  153. char *q;
  154. int i;
  155. if (ioctl(dev_fd, SCSI_IOCTL_GET_IDLUN, &id))
  156. return NULL;
  157. sprintf(buffer,
  158. "Host: scsi%u Channel: %02u Id: %02u Lun: %02u\n",
  159. /* This is very wrong (works only if you have one HBA),
  160. but I haven't found a way how to get hostno
  161. from the current kernel */
  162. 0,
  163. (id[0]>>16) & 0xff,
  164. id[0] & 0xff,
  165. (id[0]>>8) & 0xff
  166. );
  167. pfd = fopen_for_read("/proc/scsi/scsi");
  168. if (!pfd) {
  169. return NULL;
  170. }
  171. while (fgets(buffer2, 2048, pfd)) {
  172. if (strcmp(buffer, buffer2))
  173. continue;
  174. if (!fgets(buffer2, 2048, pfd))
  175. break;
  176. q = strstr(buffer2, "Vendor: ");
  177. if (!q)
  178. break;
  179. q += 8;
  180. vendor = q;
  181. q = strstr(q, " ");
  182. *q++ = '\0'; /* truncate vendor name */
  183. q = strstr(q, "Model: ");
  184. if (!q)
  185. break;
  186. *q = '\0';
  187. q += 7;
  188. model = q;
  189. q = strstr(q, " Rev: ");
  190. if (!q)
  191. break;
  192. *q = '\0';
  193. for (i = 0; i < ARRAY_SIZE(sun_drives); i++) {
  194. if (*sun_drives[i].vendor && strcasecmp(sun_drives[i].vendor, vendor))
  195. continue;
  196. if (!strstr(model, sun_drives[i].model))
  197. continue;
  198. printf("Autoconfigure found a %s%s%s\n",
  199. sun_drives[i].vendor,
  200. (*sun_drives[i].vendor) ? " " : "",
  201. sun_drives[i].model);
  202. p = sun_drives + i;
  203. break;
  204. }
  205. break;
  206. }
  207. fclose(pfd);
  208. #endif
  209. return p;
  210. }
  211. static void
  212. create_sunlabel(void)
  213. {
  214. struct hd_geometry geometry;
  215. unsigned ndiv;
  216. unsigned char c;
  217. const struct sun_predefined_drives *p = NULL;
  218. printf(msg_building_new_label, "sun disklabel");
  219. sun_other_endian = BB_LITTLE_ENDIAN;
  220. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  221. sunlabel->magic = SUN_SSWAP16(SUN_LABEL_MAGIC);
  222. if (!floppy) {
  223. unsigned i;
  224. puts("Drive type\n"
  225. " ? auto configure\n"
  226. " 0 custom (with hardware detected defaults)");
  227. for (i = 0; i < ARRAY_SIZE(sun_drives); i++) {
  228. printf(" %c %s%s%s\n",
  229. i + 'a', sun_drives[i].vendor,
  230. (*sun_drives[i].vendor) ? " " : "",
  231. sun_drives[i].model);
  232. }
  233. while (1) {
  234. c = read_nonempty("Select type (? for auto, 0 for custom): ");
  235. if (c == '0') {
  236. break;
  237. }
  238. if (c >= 'a' && c < 'a' + ARRAY_SIZE(sun_drives)) {
  239. p = sun_drives + c - 'a';
  240. break;
  241. }
  242. if (c >= 'A' && c < 'A' + ARRAY_SIZE(sun_drives)) {
  243. p = sun_drives + c - 'A';
  244. break;
  245. }
  246. if (c == '?' && scsi_disk) {
  247. p = sun_autoconfigure_scsi();
  248. if (p)
  249. break;
  250. printf("Autoconfigure failed\n");
  251. }
  252. }
  253. }
  254. if (!p || floppy) {
  255. if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
  256. g_heads = geometry.heads;
  257. g_sectors = geometry.sectors;
  258. g_cylinders = geometry.cylinders;
  259. } else {
  260. g_heads = 0;
  261. g_sectors = 0;
  262. g_cylinders = 0;
  263. }
  264. if (floppy) {
  265. sunlabel->nacyl = 0;
  266. sunlabel->pcylcount = SUN_SSWAP16(g_cylinders);
  267. sunlabel->rspeed = SUN_SSWAP16(300);
  268. sunlabel->ilfact = SUN_SSWAP16(1);
  269. sunlabel->sparecyl = 0;
  270. } else {
  271. g_heads = read_int(1, g_heads, 1024, 0, "Heads");
  272. g_sectors = read_int(1, g_sectors, 1024, 0, "Sectors/track");
  273. if (g_cylinders)
  274. g_cylinders = read_int(1, g_cylinders - 2, 65535, 0, "Cylinders");
  275. else
  276. g_cylinders = read_int(1, 0, 65535, 0, "Cylinders");
  277. sunlabel->nacyl = SUN_SSWAP16(read_int(0, 2, 65535, 0, "Alternate cylinders"));
  278. sunlabel->pcylcount = SUN_SSWAP16(read_int(0, g_cylinders + SUN_SSWAP16(sunlabel->nacyl), 65535, 0, "Physical cylinders"));
  279. sunlabel->rspeed = SUN_SSWAP16(read_int(1, 5400, 100000, 0, "Rotation speed (rpm)"));
  280. sunlabel->ilfact = SUN_SSWAP16(read_int(1, 1, 32, 0, "Interleave factor"));
  281. sunlabel->sparecyl = SUN_SSWAP16(read_int(0, 0, g_sectors, 0, "Extra sectors per cylinder"));
  282. }
  283. } else {
  284. sunlabel->sparecyl = SUN_SSWAP16(p->sparecyl);
  285. sunlabel->ncyl = SUN_SSWAP16(p->ncyl);
  286. sunlabel->nacyl = SUN_SSWAP16(p->nacyl);
  287. sunlabel->pcylcount = SUN_SSWAP16(p->pcylcount);
  288. sunlabel->ntrks = SUN_SSWAP16(p->ntrks);
  289. sunlabel->nsect = SUN_SSWAP16(p->nsect);
  290. sunlabel->rspeed = SUN_SSWAP16(p->rspeed);
  291. sunlabel->ilfact = SUN_SSWAP16(1);
  292. g_cylinders = p->ncyl;
  293. g_heads = p->ntrks;
  294. g_sectors = p->nsect;
  295. puts("You may change all the disk params from the x menu");
  296. }
  297. snprintf((char *)(sunlabel->info), sizeof(sunlabel->info),
  298. "%s%s%s cyl %u alt %u hd %u sec %u",
  299. p ? p->vendor : "", (p && *p->vendor) ? " " : "",
  300. p ? p->model : (floppy ? "3,5\" floppy" : "Linux custom"),
  301. g_cylinders, SUN_SSWAP16(sunlabel->nacyl), g_heads, g_sectors);
  302. sunlabel->ntrks = SUN_SSWAP16(g_heads);
  303. sunlabel->nsect = SUN_SSWAP16(g_sectors);
  304. sunlabel->ncyl = SUN_SSWAP16(g_cylinders);
  305. if (floppy)
  306. set_sun_partition(0, 0, g_cylinders * g_heads * g_sectors, LINUX_NATIVE);
  307. else {
  308. if (g_cylinders * g_heads * g_sectors >= 150 * 2048) {
  309. ndiv = g_cylinders - (50 * 2048 / (g_heads * g_sectors)); /* 50M swap */
  310. } else
  311. ndiv = g_cylinders * 2 / 3;
  312. set_sun_partition(0, 0, ndiv * g_heads * g_sectors, LINUX_NATIVE);
  313. set_sun_partition(1, ndiv * g_heads * g_sectors, g_cylinders * g_heads * g_sectors, LINUX_SWAP);
  314. sunlabel->infos[1].flags |= 0x01; /* Not mountable */
  315. }
  316. set_sun_partition(2, 0, g_cylinders * g_heads * g_sectors, SUN_WHOLE_DISK);
  317. {
  318. unsigned short *ush = (unsigned short *)sunlabel;
  319. unsigned short csum = 0;
  320. while (ush < (unsigned short *)(&sunlabel->csum))
  321. csum ^= *ush++;
  322. sunlabel->csum = csum;
  323. }
  324. set_all_unchanged();
  325. set_changed(0);
  326. check_sun_label();
  327. get_boot(CREATE_EMPTY_SUN);
  328. }
  329. static void
  330. toggle_sunflags(int i, unsigned char mask)
  331. {
  332. if (sunlabel->infos[i].flags & mask)
  333. sunlabel->infos[i].flags &= ~mask;
  334. else
  335. sunlabel->infos[i].flags |= mask;
  336. set_changed(i);
  337. }
  338. static void
  339. fetch_sun(unsigned *starts, unsigned *lens, unsigned *start, unsigned *stop)
  340. {
  341. int i, continuous = 1;
  342. *start = 0;
  343. *stop = g_cylinders * g_heads * g_sectors;
  344. for (i = 0; i < g_partitions; i++) {
  345. if (sunlabel->partitions[i].num_sectors
  346. && sunlabel->infos[i].id
  347. && sunlabel->infos[i].id != SUN_WHOLE_DISK) {
  348. starts[i] = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * g_heads * g_sectors;
  349. lens[i] = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  350. if (continuous) {
  351. if (starts[i] == *start)
  352. *start += lens[i];
  353. else if (starts[i] + lens[i] >= *stop)
  354. *stop = starts[i];
  355. else
  356. continuous = 0;
  357. /* There will be probably more gaps
  358. than one, so lets check afterwards */
  359. }
  360. } else {
  361. starts[i] = 0;
  362. lens[i] = 0;
  363. }
  364. }
  365. }
  366. static unsigned *verify_sun_starts;
  367. static int
  368. verify_sun_cmp(int *a, int *b)
  369. {
  370. if (*a == -1) return 1;
  371. if (*b == -1) return -1;
  372. if (verify_sun_starts[*a] > verify_sun_starts[*b]) return 1;
  373. return -1;
  374. }
  375. static void
  376. verify_sun(void)
  377. {
  378. unsigned starts[8], lens[8], start, stop;
  379. int i,j,k,starto,endo;
  380. int array[8];
  381. verify_sun_starts = starts;
  382. fetch_sun(starts, lens, &start, &stop);
  383. for (k = 0; k < 7; k++) {
  384. for (i = 0; i < 8; i++) {
  385. if (k && (lens[i] % (g_heads * g_sectors))) {
  386. printf("Partition %u doesn't end on cylinder boundary\n", i+1);
  387. }
  388. if (lens[i]) {
  389. for (j = 0; j < i; j++)
  390. if (lens[j]) {
  391. if (starts[j] == starts[i]+lens[i]) {
  392. starts[j] = starts[i]; lens[j] += lens[i];
  393. lens[i] = 0;
  394. } else if (starts[i] == starts[j]+lens[j]){
  395. lens[j] += lens[i];
  396. lens[i] = 0;
  397. } else if (!k) {
  398. if (starts[i] < starts[j]+lens[j]
  399. && starts[j] < starts[i]+lens[i]) {
  400. starto = starts[i];
  401. if (starts[j] > starto)
  402. starto = starts[j];
  403. endo = starts[i]+lens[i];
  404. if (starts[j]+lens[j] < endo)
  405. endo = starts[j]+lens[j];
  406. printf("Partition %u overlaps with others in "
  407. "sectors %u-%u\n", i+1, starto, endo);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. for (i = 0; i < 8; i++) {
  415. if (lens[i])
  416. array[i] = i;
  417. else
  418. array[i] = -1;
  419. }
  420. qsort(array, ARRAY_SIZE(array), sizeof(array[0]),
  421. (int (*)(const void *,const void *)) verify_sun_cmp);
  422. if (array[0] == -1) {
  423. printf("No partitions defined\n");
  424. return;
  425. }
  426. stop = g_cylinders * g_heads * g_sectors;
  427. if (starts[array[0]])
  428. printf("Unused gap - sectors %u-%u\n", 0, starts[array[0]]);
  429. for (i = 0; i < 7 && array[i+1] != -1; i++) {
  430. printf("Unused gap - sectors %u-%u\n", starts[array[i]]+lens[array[i]], starts[array[i+1]]);
  431. }
  432. start = starts[array[i]] + lens[array[i]];
  433. if (start < stop)
  434. printf("Unused gap - sectors %u-%u\n", start, stop);
  435. }
  436. static void
  437. add_sun_partition(int n, int sys)
  438. {
  439. unsigned start, stop, stop2;
  440. unsigned starts[8], lens[8];
  441. int whole_disk = 0;
  442. char mesg[256];
  443. int i, first, last;
  444. if (sunlabel->partitions[n].num_sectors && sunlabel->infos[n].id) {
  445. printf(msg_part_already_defined, n + 1);
  446. return;
  447. }
  448. fetch_sun(starts, lens, &start, &stop);
  449. if (stop <= start) {
  450. if (n == 2)
  451. whole_disk = 1;
  452. else {
  453. printf("Other partitions already cover the whole disk.\n"
  454. "Delete/shrink them before retry.\n");
  455. return;
  456. }
  457. }
  458. snprintf(mesg, sizeof(mesg), "First %s", str_units());
  459. while (1) {
  460. if (whole_disk)
  461. first = read_int(0, 0, 0, 0, mesg);
  462. else
  463. first = read_int(scround(start), scround(stop)+1,
  464. scround(stop), 0, mesg);
  465. if (display_in_cyl_units) {
  466. first *= units_per_sector;
  467. } else {
  468. /* Starting sector has to be properly aligned */
  469. first = (first + g_heads * g_sectors - 1) /
  470. (g_heads * g_sectors);
  471. first *= g_heads * g_sectors;
  472. }
  473. if (n == 2 && first != 0)
  474. printf("\
  475. It is highly recommended that the third partition covers the whole disk\n\
  476. and is of type 'Whole disk'\n");
  477. /* ewt asks to add: "don't start a partition at cyl 0"
  478. However, edmundo@rano.demon.co.uk writes:
  479. "In addition to having a Sun partition table, to be able to
  480. boot from the disc, the first partition, /dev/sdX1, must
  481. start at cylinder 0. This means that /dev/sdX1 contains
  482. the partition table and the boot block, as these are the
  483. first two sectors of the disc. Therefore you must be
  484. careful what you use /dev/sdX1 for. In particular, you must
  485. not use a partition starting at cylinder 0 for Linux swap,
  486. as that would overwrite the partition table and the boot
  487. block. You may, however, use such a partition for a UFS
  488. or EXT2 file system, as these file systems leave the first
  489. 1024 bytes undisturbed. */
  490. /* On the other hand, one should not use partitions
  491. starting at block 0 in an md, or the label will
  492. be trashed. */
  493. for (i = 0; i < g_partitions; i++)
  494. if (lens[i] && starts[i] <= first && starts[i] + lens[i] > first)
  495. break;
  496. if (i < g_partitions && !whole_disk) {
  497. if (n == 2 && !first) {
  498. whole_disk = 1;
  499. break;
  500. }
  501. printf("Sector %u is already allocated\n", first);
  502. } else
  503. break;
  504. }
  505. stop = g_cylinders * g_heads * g_sectors;
  506. stop2 = stop;
  507. for (i = 0; i < g_partitions; i++) {
  508. if (starts[i] > first && starts[i] < stop)
  509. stop = starts[i];
  510. }
  511. snprintf(mesg, sizeof(mesg),
  512. "Last %s or +size or +sizeM or +sizeK",
  513. str_units());
  514. if (whole_disk)
  515. last = read_int(scround(stop2), scround(stop2), scround(stop2),
  516. 0, mesg);
  517. else if (n == 2 && !first)
  518. last = read_int(scround(first), scround(stop2), scround(stop2),
  519. scround(first), mesg);
  520. else
  521. last = read_int(scround(first), scround(stop), scround(stop),
  522. scround(first), mesg);
  523. if (display_in_cyl_units)
  524. last *= units_per_sector;
  525. if (n == 2 && !first) {
  526. if (last >= stop2) {
  527. whole_disk = 1;
  528. last = stop2;
  529. } else if (last > stop) {
  530. printf(
  531. "You haven't covered the whole disk with the 3rd partition,\n"
  532. "but your value %u %s covers some other partition.\n"
  533. "Your entry has been changed to %u %s\n",
  534. scround(last), str_units(),
  535. scround(stop), str_units());
  536. last = stop;
  537. }
  538. } else if (!whole_disk && last > stop)
  539. last = stop;
  540. if (whole_disk)
  541. sys = SUN_WHOLE_DISK;
  542. set_sun_partition(n, first, last, sys);
  543. }
  544. static void
  545. sun_delete_partition(int i)
  546. {
  547. unsigned int nsec;
  548. if (i == 2
  549. && sunlabel->infos[i].id == SUN_WHOLE_DISK
  550. && !sunlabel->partitions[i].start_cylinder
  551. && (nsec = SUN_SSWAP32(sunlabel->partitions[i].num_sectors)) == g_heads * g_sectors * g_cylinders)
  552. printf("If you want to maintain SunOS/Solaris compatibility, "
  553. "consider leaving this\n"
  554. "partition as Whole disk (5), starting at 0, with %u "
  555. "sectors\n", nsec);
  556. sunlabel->infos[i].id = 0;
  557. sunlabel->partitions[i].num_sectors = 0;
  558. }
  559. static void
  560. sun_change_sysid(int i, int sys)
  561. {
  562. if (sys == LINUX_SWAP && !sunlabel->partitions[i].start_cylinder) {
  563. read_maybe_empty(
  564. "It is highly recommended that the partition at offset 0\n"
  565. "is UFS, EXT2FS filesystem or SunOS swap. Putting Linux swap\n"
  566. "there may destroy your partition table and bootblock.\n"
  567. "Type YES if you're very sure you would like that partition\n"
  568. "tagged with 82 (Linux swap): ");
  569. if (strcmp(line_ptr, "YES") != 0)
  570. return;
  571. }
  572. switch (sys) {
  573. case SUNOS_SWAP:
  574. case LINUX_SWAP:
  575. /* swaps are not mountable by default */
  576. sunlabel->infos[i].flags |= 0x01;
  577. break;
  578. default:
  579. /* assume other types are mountable;
  580. user can change it anyway */
  581. sunlabel->infos[i].flags &= ~0x01;
  582. break;
  583. }
  584. sunlabel->infos[i].id = sys;
  585. }
  586. static void
  587. sun_list_table(int xtra)
  588. {
  589. int i, w;
  590. w = strlen(disk_device);
  591. if (xtra)
  592. printf(
  593. "\nDisk %s (Sun disk label): %u heads, %u sectors, %u rpm\n"
  594. "%u cylinders, %u alternate cylinders, %u physical cylinders\n"
  595. "%u extra sects/cyl, interleave %u:1\n"
  596. "%s\n"
  597. "Units = %ss of %u * 512 bytes\n\n",
  598. disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed),
  599. g_cylinders, SUN_SSWAP16(sunlabel->nacyl),
  600. SUN_SSWAP16(sunlabel->pcylcount),
  601. SUN_SSWAP16(sunlabel->sparecyl),
  602. SUN_SSWAP16(sunlabel->ilfact),
  603. (char *)sunlabel,
  604. str_units(), units_per_sector);
  605. else
  606. printf(
  607. "\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n"
  608. "Units = %ss of %u * 512 bytes\n\n",
  609. disk_device, g_heads, g_sectors, g_cylinders,
  610. str_units(), units_per_sector);
  611. printf("%*s Flag Start End Blocks Id System\n",
  612. w + 1, "Device");
  613. for (i = 0; i < g_partitions; i++) {
  614. if (sunlabel->partitions[i].num_sectors) {
  615. uint32_t start = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * g_heads * g_sectors;
  616. uint32_t len = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  617. printf("%s %c%c %9lu %9lu %9lu%c %2x %s\n",
  618. partname(disk_device, i+1, w), /* device */
  619. (sunlabel->infos[i].flags & 0x01) ? 'u' : ' ', /* flags */
  620. (sunlabel->infos[i].flags & 0x10) ? 'r' : ' ',
  621. (long) scround(start), /* start */
  622. (long) scround(start+len), /* end */
  623. (long) len / 2, len & 1 ? '+' : ' ', /* odd flag on end */
  624. sunlabel->infos[i].id, /* type id */
  625. partition_type(sunlabel->infos[i].id)); /* type name */
  626. }
  627. }
  628. }
  629. #if ENABLE_FEATURE_FDISK_ADVANCED
  630. static void
  631. sun_set_alt_cyl(void)
  632. {
  633. sunlabel->nacyl =
  634. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->nacyl), 65535, 0,
  635. "Number of alternate cylinders"));
  636. }
  637. static void
  638. sun_set_ncyl(int cyl)
  639. {
  640. sunlabel->ncyl = SUN_SSWAP16(cyl);
  641. }
  642. static void
  643. sun_set_xcyl(void)
  644. {
  645. sunlabel->sparecyl =
  646. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->sparecyl), g_sectors, 0,
  647. "Extra sectors per cylinder"));
  648. }
  649. static void
  650. sun_set_ilfact(void)
  651. {
  652. sunlabel->ilfact =
  653. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->ilfact), 32, 0,
  654. "Interleave factor"));
  655. }
  656. static void
  657. sun_set_rspeed(void)
  658. {
  659. sunlabel->rspeed =
  660. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->rspeed), 100000, 0,
  661. "Rotation speed (rpm)"));
  662. }
  663. static void
  664. sun_set_pcylcount(void)
  665. {
  666. sunlabel->pcylcount =
  667. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->pcylcount), 65535, 0,
  668. "Number of physical cylinders"));
  669. }
  670. #endif /* FEATURE_FDISK_ADVANCED */
  671. static void
  672. sun_write_table(void)
  673. {
  674. unsigned short *ush = (unsigned short *)sunlabel;
  675. unsigned short csum = 0;
  676. while (ush < (unsigned short *)(&sunlabel->csum))
  677. csum ^= *ush++;
  678. sunlabel->csum = csum;
  679. write_sector(0, sunlabel);
  680. }
  681. #endif /* SUN_LABEL */