fdisk_sun.c 20 KB

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