3
0

fdisk_sun.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 tarball for details.
  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[] = {
  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[] = {
  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. get_boot(CREATE_EMPTY_SUN);
  327. }
  328. static void
  329. toggle_sunflags(int i, unsigned char mask)
  330. {
  331. if (sunlabel->infos[i].flags & mask)
  332. sunlabel->infos[i].flags &= ~mask;
  333. else
  334. sunlabel->infos[i].flags |= mask;
  335. set_changed(i);
  336. }
  337. static void
  338. fetch_sun(unsigned *starts, unsigned *lens, unsigned *start, unsigned *stop)
  339. {
  340. int i, continuous = 1;
  341. *start = 0;
  342. *stop = g_cylinders * g_heads * g_sectors;
  343. for (i = 0; i < g_partitions; i++) {
  344. if (sunlabel->partitions[i].num_sectors
  345. && sunlabel->infos[i].id
  346. && sunlabel->infos[i].id != SUN_WHOLE_DISK) {
  347. starts[i] = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * g_heads * g_sectors;
  348. lens[i] = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  349. if (continuous) {
  350. if (starts[i] == *start)
  351. *start += lens[i];
  352. else if (starts[i] + lens[i] >= *stop)
  353. *stop = starts[i];
  354. else
  355. continuous = 0;
  356. /* There will be probably more gaps
  357. than one, so lets check afterwards */
  358. }
  359. } else {
  360. starts[i] = 0;
  361. lens[i] = 0;
  362. }
  363. }
  364. }
  365. static unsigned *verify_sun_starts;
  366. static int
  367. verify_sun_cmp(int *a, int *b)
  368. {
  369. if (*a == -1) return 1;
  370. if (*b == -1) return -1;
  371. if (verify_sun_starts[*a] > verify_sun_starts[*b]) return 1;
  372. return -1;
  373. }
  374. static void
  375. verify_sun(void)
  376. {
  377. unsigned starts[8], lens[8], start, stop;
  378. int i,j,k,starto,endo;
  379. int array[8];
  380. verify_sun_starts = starts;
  381. fetch_sun(starts, lens, &start, &stop);
  382. for (k = 0; k < 7; k++) {
  383. for (i = 0; i < 8; i++) {
  384. if (k && (lens[i] % (g_heads * g_sectors))) {
  385. printf("Partition %u doesn't end on cylinder boundary\n", i+1);
  386. }
  387. if (lens[i]) {
  388. for (j = 0; j < i; j++)
  389. if (lens[j]) {
  390. if (starts[j] == starts[i]+lens[i]) {
  391. starts[j] = starts[i]; lens[j] += lens[i];
  392. lens[i] = 0;
  393. } else if (starts[i] == starts[j]+lens[j]){
  394. lens[j] += lens[i];
  395. lens[i] = 0;
  396. } else if (!k) {
  397. if (starts[i] < starts[j]+lens[j]
  398. && starts[j] < starts[i]+lens[i]) {
  399. starto = starts[i];
  400. if (starts[j] > starto)
  401. starto = starts[j];
  402. endo = starts[i]+lens[i];
  403. if (starts[j]+lens[j] < endo)
  404. endo = starts[j]+lens[j];
  405. printf("Partition %u overlaps with others in "
  406. "sectors %u-%u\n", i+1, starto, endo);
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. for (i = 0; i < 8; i++) {
  414. if (lens[i])
  415. array[i] = i;
  416. else
  417. array[i] = -1;
  418. }
  419. qsort(array, ARRAY_SIZE(array), sizeof(array[0]),
  420. (int (*)(const void *,const void *)) verify_sun_cmp);
  421. if (array[0] == -1) {
  422. printf("No partitions defined\n");
  423. return;
  424. }
  425. stop = g_cylinders * g_heads * g_sectors;
  426. if (starts[array[0]])
  427. printf("Unused gap - sectors %u-%u\n", 0, starts[array[0]]);
  428. for (i = 0; i < 7 && array[i+1] != -1; i++) {
  429. printf("Unused gap - sectors %u-%u\n", starts[array[i]]+lens[array[i]], starts[array[i+1]]);
  430. }
  431. start = starts[array[i]] + lens[array[i]];
  432. if (start < stop)
  433. printf("Unused gap - sectors %u-%u\n", start, stop);
  434. }
  435. static void
  436. add_sun_partition(int n, int sys)
  437. {
  438. unsigned start, stop, stop2;
  439. unsigned starts[8], lens[8];
  440. int whole_disk = 0;
  441. char mesg[256];
  442. int i, first, last;
  443. if (sunlabel->partitions[n].num_sectors && sunlabel->infos[n].id) {
  444. printf(msg_part_already_defined, n + 1);
  445. return;
  446. }
  447. fetch_sun(starts,lens,&start,&stop);
  448. if (stop <= start) {
  449. if (n == 2)
  450. whole_disk = 1;
  451. else {
  452. printf("Other partitions already cover the whole disk.\n"
  453. "Delete/shrink them before retry.\n");
  454. return;
  455. }
  456. }
  457. snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
  458. while (1) {
  459. if (whole_disk)
  460. first = read_int(0, 0, 0, 0, mesg);
  461. else
  462. first = read_int(scround(start), scround(stop)+1,
  463. scround(stop), 0, mesg);
  464. if (display_in_cyl_units)
  465. first *= units_per_sector;
  466. else
  467. /* Starting sector has to be properly aligned */
  468. first = (first + g_heads * g_sectors - 1) / (g_heads * g_sectors);
  469. if (n == 2 && first != 0)
  470. printf("\
  471. It is highly recommended that the third partition covers the whole disk\n\
  472. and is of type 'Whole disk'\n");
  473. /* ewt asks to add: "don't start a partition at cyl 0"
  474. However, edmundo@rano.demon.co.uk writes:
  475. "In addition to having a Sun partition table, to be able to
  476. boot from the disc, the first partition, /dev/sdX1, must
  477. start at cylinder 0. This means that /dev/sdX1 contains
  478. the partition table and the boot block, as these are the
  479. first two sectors of the disc. Therefore you must be
  480. careful what you use /dev/sdX1 for. In particular, you must
  481. not use a partition starting at cylinder 0 for Linux swap,
  482. as that would overwrite the partition table and the boot
  483. block. You may, however, use such a partition for a UFS
  484. or EXT2 file system, as these file systems leave the first
  485. 1024 bytes undisturbed. */
  486. /* On the other hand, one should not use partitions
  487. starting at block 0 in an md, or the label will
  488. be trashed. */
  489. for (i = 0; i < g_partitions; i++)
  490. if (lens[i] && starts[i] <= first && starts[i] + lens[i] > first)
  491. break;
  492. if (i < g_partitions && !whole_disk) {
  493. if (n == 2 && !first) {
  494. whole_disk = 1;
  495. break;
  496. }
  497. printf("Sector %u is already allocated\n", first);
  498. } else
  499. break;
  500. }
  501. stop = g_cylinders * g_heads * g_sectors;
  502. stop2 = stop;
  503. for (i = 0; i < g_partitions; i++) {
  504. if (starts[i] > first && starts[i] < stop)
  505. stop = starts[i];
  506. }
  507. snprintf(mesg, sizeof(mesg),
  508. "Last %s or +size or +sizeM or +sizeK",
  509. str_units(SINGULAR));
  510. if (whole_disk)
  511. last = read_int(scround(stop2), scround(stop2), scround(stop2),
  512. 0, mesg);
  513. else if (n == 2 && !first)
  514. last = read_int(scround(first), scround(stop2), scround(stop2),
  515. scround(first), mesg);
  516. else
  517. last = read_int(scround(first), scround(stop), scround(stop),
  518. scround(first), mesg);
  519. if (display_in_cyl_units)
  520. last *= units_per_sector;
  521. if (n == 2 && !first) {
  522. if (last >= stop2) {
  523. whole_disk = 1;
  524. last = stop2;
  525. } else if (last > stop) {
  526. printf(
  527. "You haven't covered the whole disk with the 3rd partition,\n"
  528. "but your value %u %s covers some other partition.\n"
  529. "Your entry has been changed to %u %s\n",
  530. scround(last), str_units(SINGULAR),
  531. scround(stop), str_units(SINGULAR));
  532. last = stop;
  533. }
  534. } else if (!whole_disk && last > stop)
  535. last = stop;
  536. if (whole_disk)
  537. sys = SUN_WHOLE_DISK;
  538. set_sun_partition(n, first, last, sys);
  539. }
  540. static void
  541. sun_delete_partition(int i)
  542. {
  543. unsigned int nsec;
  544. if (i == 2
  545. && sunlabel->infos[i].id == SUN_WHOLE_DISK
  546. && !sunlabel->partitions[i].start_cylinder
  547. && (nsec = SUN_SSWAP32(sunlabel->partitions[i].num_sectors)) == g_heads * g_sectors * g_cylinders)
  548. printf("If you want to maintain SunOS/Solaris compatibility, "
  549. "consider leaving this\n"
  550. "partition as Whole disk (5), starting at 0, with %u "
  551. "sectors\n", nsec);
  552. sunlabel->infos[i].id = 0;
  553. sunlabel->partitions[i].num_sectors = 0;
  554. }
  555. static void
  556. sun_change_sysid(int i, int sys)
  557. {
  558. if (sys == LINUX_SWAP && !sunlabel->partitions[i].start_cylinder) {
  559. read_maybe_empty(
  560. "It is highly recommended that the partition at offset 0\n"
  561. "is UFS, EXT2FS filesystem or SunOS swap. Putting Linux swap\n"
  562. "there may destroy your partition table and bootblock.\n"
  563. "Type YES if you're very sure you would like that partition\n"
  564. "tagged with 82 (Linux swap): ");
  565. if (strcmp (line_ptr, "YES\n"))
  566. return;
  567. }
  568. switch (sys) {
  569. case SUNOS_SWAP:
  570. case LINUX_SWAP:
  571. /* swaps are not mountable by default */
  572. sunlabel->infos[i].flags |= 0x01;
  573. break;
  574. default:
  575. /* assume other types are mountable;
  576. user can change it anyway */
  577. sunlabel->infos[i].flags &= ~0x01;
  578. break;
  579. }
  580. sunlabel->infos[i].id = sys;
  581. }
  582. static void
  583. sun_list_table(int xtra)
  584. {
  585. int i, w;
  586. w = strlen(disk_device);
  587. if (xtra)
  588. printf(
  589. "\nDisk %s (Sun disk label): %u heads, %u sectors, %u rpm\n"
  590. "%u cylinders, %u alternate cylinders, %u physical cylinders\n"
  591. "%u extra sects/cyl, interleave %u:1\n"
  592. "%s\n"
  593. "Units = %s of %u * 512 bytes\n\n",
  594. disk_device, g_heads, g_sectors, SUN_SSWAP16(sunlabel->rspeed),
  595. g_cylinders, SUN_SSWAP16(sunlabel->nacyl),
  596. SUN_SSWAP16(sunlabel->pcylcount),
  597. SUN_SSWAP16(sunlabel->sparecyl),
  598. SUN_SSWAP16(sunlabel->ilfact),
  599. (char *)sunlabel,
  600. str_units(PLURAL), units_per_sector);
  601. else
  602. printf(
  603. "\nDisk %s (Sun disk label): %u heads, %u sectors, %u cylinders\n"
  604. "Units = %s of %u * 512 bytes\n\n",
  605. disk_device, g_heads, g_sectors, g_cylinders,
  606. str_units(PLURAL), units_per_sector);
  607. printf("%*s Flag Start End Blocks Id System\n",
  608. w + 1, "Device");
  609. for (i = 0; i < g_partitions; i++) {
  610. if (sunlabel->partitions[i].num_sectors) {
  611. uint32_t start = SUN_SSWAP32(sunlabel->partitions[i].start_cylinder) * g_heads * g_sectors;
  612. uint32_t len = SUN_SSWAP32(sunlabel->partitions[i].num_sectors);
  613. printf("%s %c%c %9lu %9lu %9lu%c %2x %s\n",
  614. partname(disk_device, i+1, w), /* device */
  615. (sunlabel->infos[i].flags & 0x01) ? 'u' : ' ', /* flags */
  616. (sunlabel->infos[i].flags & 0x10) ? 'r' : ' ',
  617. (long) scround(start), /* start */
  618. (long) scround(start+len), /* end */
  619. (long) len / 2, len & 1 ? '+' : ' ', /* odd flag on end */
  620. sunlabel->infos[i].id, /* type id */
  621. partition_type(sunlabel->infos[i].id)); /* type name */
  622. }
  623. }
  624. }
  625. #if ENABLE_FEATURE_FDISK_ADVANCED
  626. static void
  627. sun_set_alt_cyl(void)
  628. {
  629. sunlabel->nacyl =
  630. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->nacyl), 65535, 0,
  631. "Number of alternate cylinders"));
  632. }
  633. static void
  634. sun_set_ncyl(int cyl)
  635. {
  636. sunlabel->ncyl = SUN_SSWAP16(cyl);
  637. }
  638. static void
  639. sun_set_xcyl(void)
  640. {
  641. sunlabel->sparecyl =
  642. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->sparecyl), g_sectors, 0,
  643. "Extra sectors per cylinder"));
  644. }
  645. static void
  646. sun_set_ilfact(void)
  647. {
  648. sunlabel->ilfact =
  649. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->ilfact), 32, 0,
  650. "Interleave factor"));
  651. }
  652. static void
  653. sun_set_rspeed(void)
  654. {
  655. sunlabel->rspeed =
  656. SUN_SSWAP16(read_int(1, SUN_SSWAP16(sunlabel->rspeed), 100000, 0,
  657. "Rotation speed (rpm)"));
  658. }
  659. static void
  660. sun_set_pcylcount(void)
  661. {
  662. sunlabel->pcylcount =
  663. SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->pcylcount), 65535, 0,
  664. "Number of physical cylinders"));
  665. }
  666. #endif /* FEATURE_FDISK_ADVANCED */
  667. static void
  668. sun_write_table(void)
  669. {
  670. unsigned short *ush = (unsigned short *)sunlabel;
  671. unsigned short csum = 0;
  672. while (ush < (unsigned short *)(&sunlabel->csum))
  673. csum ^= *ush++;
  674. sunlabel->csum = csum;
  675. write_sector(0, sunlabel);
  676. }
  677. #endif /* SUN_LABEL */