fdisk_sgi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #ifdef CONFIG_FEATURE_SGI_LABEL
  2. /*
  3. * Copyright (C) Andreas Neuper, Sep 1998.
  4. * This file may be modified and redistributed under
  5. * the terms of the GNU Public License.
  6. */
  7. struct device_parameter { /* 48 bytes */
  8. unsigned char skew;
  9. unsigned char gap1;
  10. unsigned char gap2;
  11. unsigned char sparecyl;
  12. unsigned short pcylcount;
  13. unsigned short head_vol0;
  14. unsigned short ntrks; /* tracks in cyl 0 or vol 0 */
  15. unsigned char cmd_tag_queue_depth;
  16. unsigned char unused0;
  17. unsigned short unused1;
  18. unsigned short nsect; /* sectors/tracks in cyl 0 or vol 0 */
  19. unsigned short bytes;
  20. unsigned short ilfact;
  21. unsigned int flags; /* controller flags */
  22. unsigned int datarate;
  23. unsigned int retries_on_error;
  24. unsigned int ms_per_word;
  25. unsigned short xylogics_gap1;
  26. unsigned short xylogics_syncdelay;
  27. unsigned short xylogics_readdelay;
  28. unsigned short xylogics_gap2;
  29. unsigned short xylogics_readgate;
  30. unsigned short xylogics_writecont;
  31. };
  32. /*
  33. * controller flags
  34. */
  35. #define SECTOR_SLIP 0x01
  36. #define SECTOR_FWD 0x02
  37. #define TRACK_FWD 0x04
  38. #define TRACK_MULTIVOL 0x08
  39. #define IGNORE_ERRORS 0x10
  40. #define RESEEK 0x20
  41. #define ENABLE_CMDTAGQ 0x40
  42. typedef struct {
  43. unsigned int magic; /* expect SGI_LABEL_MAGIC */
  44. unsigned short boot_part; /* active boot partition */
  45. unsigned short swap_part; /* active swap partition */
  46. unsigned char boot_file[16]; /* name of the bootfile */
  47. struct device_parameter devparam; /* 1 * 48 bytes */
  48. struct volume_directory { /* 15 * 16 bytes */
  49. unsigned char vol_file_name[8]; /* a character array */
  50. unsigned int vol_file_start; /* number of logical block */
  51. unsigned int vol_file_size; /* number of bytes */
  52. } directory[15];
  53. struct sgi_partinfo { /* 16 * 12 bytes */
  54. unsigned int num_sectors; /* number of blocks */
  55. unsigned int start_sector; /* must be cylinder aligned */
  56. unsigned int id;
  57. } partitions[16];
  58. unsigned int csum;
  59. unsigned int fillbytes;
  60. } sgi_partition;
  61. typedef struct {
  62. unsigned int magic; /* looks like a magic number */
  63. unsigned int a2;
  64. unsigned int a3;
  65. unsigned int a4;
  66. unsigned int b1;
  67. unsigned short b2;
  68. unsigned short b3;
  69. unsigned int c[16];
  70. unsigned short d[3];
  71. unsigned char scsi_string[50];
  72. unsigned char serial[137];
  73. unsigned short check1816;
  74. unsigned char installer[225];
  75. } sgiinfo;
  76. #define SGI_LABEL_MAGIC 0x0be5a941
  77. #define SGI_LABEL_MAGIC_SWAPPED 0x41a9e50b
  78. #define SGI_INFO_MAGIC 0x00072959
  79. #define SGI_INFO_MAGIC_SWAPPED 0x59290700
  80. #define SGI_SSWAP16(x) (sgi_other_endian ? __swap16(x) : (uint16_t)(x))
  81. #define SGI_SSWAP32(x) (sgi_other_endian ? __swap32(x) : (uint32_t)(x))
  82. #define sgilabel ((sgi_partition *)MBRbuffer)
  83. #define sgiparam (sgilabel->devparam)
  84. /*
  85. *
  86. * fdisksgilabel.c
  87. *
  88. * Copyright (C) Andreas Neuper, Sep 1998.
  89. * This file may be modified and redistributed under
  90. * the terms of the GNU Public License.
  91. *
  92. * Sat Mar 20 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  93. * Internationalization
  94. */
  95. static int sgi_other_endian;
  96. static int debug;
  97. static short sgi_volumes = 1;
  98. /*
  99. * only dealing with free blocks here
  100. */
  101. typedef struct {
  102. unsigned int first;
  103. unsigned int last;
  104. } freeblocks;
  105. static freeblocks freelist[17]; /* 16 partitions can produce 17 vacant slots */
  106. static void
  107. setfreelist(int i, unsigned int f, unsigned int l)
  108. {
  109. freelist[i].first = f;
  110. freelist[i].last = l;
  111. }
  112. static void
  113. add2freelist(unsigned int f, unsigned int l)
  114. {
  115. int i;
  116. for (i = 0; i < 17 ; i++)
  117. if (freelist[i].last == 0)
  118. break;
  119. setfreelist(i, f, l);
  120. }
  121. static void
  122. clearfreelist(void)
  123. {
  124. int i;
  125. for (i = 0; i < 17 ; i++)
  126. setfreelist(i, 0, 0);
  127. }
  128. static unsigned int
  129. isinfreelist(unsigned int b)
  130. {
  131. int i;
  132. for (i = 0; i < 17 ; i++)
  133. if (freelist[i].first <= b && freelist[i].last >= b)
  134. return freelist[i].last;
  135. return 0;
  136. }
  137. /* return last vacant block of this stride (never 0). */
  138. /* the '>=' is not quite correct, but simplifies the code */
  139. /*
  140. * end of free blocks section
  141. */
  142. static const struct systypes sgi_sys_types[] = {
  143. /* SGI_VOLHDR */ { "\x00" "SGI volhdr" },
  144. /* 0x01 */ { "\x01" "SGI trkrepl" },
  145. /* 0x02 */ { "\x02" "SGI secrepl" },
  146. /* SGI_SWAP */ { "\x03" "SGI raw" },
  147. /* 0x04 */ { "\x04" "SGI bsd" },
  148. /* 0x05 */ { "\x05" "SGI sysv" },
  149. /* SGI_ENTIRE_DISK */ { "\x06" "SGI volume" },
  150. /* SGI_EFS */ { "\x07" "SGI efs" },
  151. /* 0x08 */ { "\x08" "SGI lvol" },
  152. /* 0x09 */ { "\x09" "SGI rlvol" },
  153. /* SGI_XFS */ { "\x0a" "SGI xfs" },
  154. /* SGI_XFSLOG */ { "\x0b" "SGI xfslog" },
  155. /* SGI_XLV */ { "\x0c" "SGI xlv" },
  156. /* SGI_XVM */ { "\x0d" "SGI xvm" },
  157. /* LINUX_SWAP */ { "\x82" "Linux swap" },
  158. /* LINUX_NATIVE */ { "\x83" "Linux native" },
  159. /* LINUX_LVM */ { "\x8d" "Linux LVM" },
  160. /* LINUX_RAID */ { "\xfd" "Linux RAID" },
  161. { NULL }
  162. };
  163. static int
  164. sgi_get_nsect(void)
  165. {
  166. return SGI_SSWAP16(sgilabel->devparam.nsect);
  167. }
  168. static int
  169. sgi_get_ntrks(void)
  170. {
  171. return SGI_SSWAP16(sgilabel->devparam.ntrks);
  172. }
  173. static unsigned int
  174. two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */)
  175. {
  176. int i = 0;
  177. unsigned int sum = 0;
  178. size /= sizeof(unsigned int);
  179. for (i = 0; i < size; i++)
  180. sum -= SGI_SSWAP32(base[i]);
  181. return sum;
  182. }
  183. static int
  184. check_sgi_label(void)
  185. {
  186. if (sizeof(sgilabel) > 512) {
  187. fprintf(stderr,
  188. _("According to MIPS Computer Systems, Inc the "
  189. "Label must not contain more than 512 bytes\n"));
  190. exit(1);
  191. }
  192. if (sgilabel->magic != SGI_LABEL_MAGIC
  193. && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED) {
  194. current_label_type = label_dos;
  195. return 0;
  196. }
  197. sgi_other_endian = (sgilabel->magic == SGI_LABEL_MAGIC_SWAPPED);
  198. /*
  199. * test for correct checksum
  200. */
  201. if (two_s_complement_32bit_sum((unsigned int*)sgilabel,
  202. sizeof(*sgilabel))) {
  203. fprintf(stderr,
  204. _("Detected sgi disklabel with wrong checksum.\n"));
  205. }
  206. update_units();
  207. current_label_type = label_sgi;
  208. partitions = 16;
  209. sgi_volumes = 15;
  210. return 1;
  211. }
  212. static unsigned int
  213. sgi_get_start_sector(int i)
  214. {
  215. return SGI_SSWAP32(sgilabel->partitions[i].start_sector);
  216. }
  217. static unsigned int
  218. sgi_get_num_sectors(int i)
  219. {
  220. return SGI_SSWAP32(sgilabel->partitions[i].num_sectors);
  221. }
  222. static int
  223. sgi_get_sysid(int i)
  224. {
  225. return SGI_SSWAP32(sgilabel->partitions[i].id);
  226. }
  227. static int
  228. sgi_get_bootpartition(void)
  229. {
  230. return SGI_SSWAP16(sgilabel->boot_part);
  231. }
  232. static int
  233. sgi_get_swappartition(void)
  234. {
  235. return SGI_SSWAP16(sgilabel->swap_part);
  236. }
  237. static void
  238. sgi_list_table(int xtra)
  239. {
  240. int i, w, wd;
  241. int kpi = 0; /* kernel partition ID */
  242. if(xtra) {
  243. printf(_("\nDisk %s (SGI disk label): %d heads, %d sectors\n"
  244. "%d cylinders, %d physical cylinders\n"
  245. "%d extra sects/cyl, interleave %d:1\n"
  246. "%s\n"
  247. "Units = %s of %d * 512 bytes\n\n"),
  248. disk_device, heads, sectors, cylinders,
  249. SGI_SSWAP16(sgiparam.pcylcount),
  250. SGI_SSWAP16(sgiparam.sparecyl),
  251. SGI_SSWAP16(sgiparam.ilfact),
  252. (char *)sgilabel,
  253. str_units(PLURAL), units_per_sector);
  254. } else {
  255. printf( _("\nDisk %s (SGI disk label): "
  256. "%d heads, %d sectors, %d cylinders\n"
  257. "Units = %s of %d * 512 bytes\n\n"),
  258. disk_device, heads, sectors, cylinders,
  259. str_units(PLURAL), units_per_sector );
  260. }
  261. w = strlen(disk_device);
  262. wd = strlen(_("Device"));
  263. if (w < wd)
  264. w = wd;
  265. printf(_("----- partitions -----\n"
  266. "Pt# %*s Info Start End Sectors Id System\n"),
  267. w + 2, _("Device"));
  268. for (i = 0 ; i < partitions; i++) {
  269. if( sgi_get_num_sectors(i) || debug ) {
  270. uint32_t start = sgi_get_start_sector(i);
  271. uint32_t len = sgi_get_num_sectors(i);
  272. kpi++; /* only count nonempty partitions */
  273. printf(
  274. "%2d: %s %4s %9ld %9ld %9ld %2x %s\n",
  275. /* fdisk part number */ i+1,
  276. /* device */ partname(disk_device, kpi, w+3),
  277. /* flags */ (sgi_get_swappartition() == i) ? "swap" :
  278. /* flags */ (sgi_get_bootpartition() == i) ? "boot" : " ",
  279. /* start */ (long) scround(start),
  280. /* end */ (long) scround(start+len)-1,
  281. /* no odd flag on end */(long) len,
  282. /* type id */ sgi_get_sysid(i),
  283. /* type name */ partition_type(sgi_get_sysid(i)));
  284. }
  285. }
  286. printf(_("----- Bootinfo -----\nBootfile: %s\n"
  287. "----- Directory Entries -----\n"),
  288. sgilabel->boot_file);
  289. for (i = 0 ; i < sgi_volumes; i++) {
  290. if (sgilabel->directory[i].vol_file_size) {
  291. uint32_t start = SGI_SSWAP32(sgilabel->directory[i].vol_file_start);
  292. uint32_t len = SGI_SSWAP32(sgilabel->directory[i].vol_file_size);
  293. unsigned char *name = sgilabel->directory[i].vol_file_name;
  294. printf(_("%2d: %-10s sector%5u size%8u\n"),
  295. i, (char*)name, (unsigned int) start, (unsigned int) len);
  296. }
  297. }
  298. }
  299. static void
  300. sgi_set_bootpartition(int i)
  301. {
  302. sgilabel->boot_part = SGI_SSWAP16(((short)i));
  303. }
  304. static unsigned int
  305. sgi_get_lastblock(void)
  306. {
  307. return heads * sectors * cylinders;
  308. }
  309. static void
  310. sgi_set_swappartition(int i)
  311. {
  312. sgilabel->swap_part = SGI_SSWAP16(((short)i));
  313. }
  314. static int
  315. sgi_check_bootfile(const char* aFile)
  316. {
  317. if (strlen(aFile) < 3) /* "/a\n" is minimum */ {
  318. printf(_("\nInvalid Bootfile!\n"
  319. "\tThe bootfile must be an absolute non-zero pathname,\n"
  320. "\te.g. \"/unix\" or \"/unix.save\".\n"));
  321. return 0;
  322. } else {
  323. if (strlen(aFile) > 16) {
  324. printf(_("\n\tName of Bootfile too long: "
  325. "16 bytes maximum.\n"));
  326. return 0;
  327. } else {
  328. if (aFile[0] != '/') {
  329. printf(_("\n\tBootfile must have a "
  330. "fully qualified pathname.\n"));
  331. return 0;
  332. }
  333. }
  334. }
  335. if (strncmp(aFile, (char*)sgilabel->boot_file, 16)) {
  336. printf(_("\n\tBe aware, that the bootfile is not checked for existence.\n\t"
  337. "SGI's default is \"/unix\" and for backup \"/unix.save\".\n"));
  338. /* filename is correct and did change */
  339. return 1;
  340. }
  341. return 0; /* filename did not change */
  342. }
  343. static const char *
  344. sgi_get_bootfile(void)
  345. {
  346. return (char*)sgilabel->boot_file;
  347. }
  348. static void
  349. sgi_set_bootfile(const char* aFile)
  350. {
  351. int i = 0;
  352. if (sgi_check_bootfile(aFile)) {
  353. while (i < 16) {
  354. if ((aFile[i] != '\n') /* in principle caught again by next line */
  355. && (strlen(aFile) > i))
  356. sgilabel->boot_file[i] = aFile[i];
  357. else
  358. sgilabel->boot_file[i] = 0;
  359. i++;
  360. }
  361. printf(_("\n\tBootfile is changed to \"%s\".\n"), sgilabel->boot_file);
  362. }
  363. }
  364. static void
  365. create_sgiinfo(void)
  366. {
  367. /* I keep SGI's habit to write the sgilabel to the second block */
  368. sgilabel->directory[0].vol_file_start = SGI_SSWAP32(2);
  369. sgilabel->directory[0].vol_file_size = SGI_SSWAP32(sizeof(sgiinfo));
  370. strncpy((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8);
  371. }
  372. static sgiinfo *fill_sgiinfo(void);
  373. static void
  374. sgi_write_table(void)
  375. {
  376. sgilabel->csum = 0;
  377. sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum(
  378. (unsigned int*)sgilabel, sizeof(*sgilabel)));
  379. assert(two_s_complement_32bit_sum(
  380. (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
  381. if (lseek(fd, 0, SEEK_SET) < 0)
  382. fdisk_fatal(unable_to_seek);
  383. if (write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
  384. fdisk_fatal(unable_to_write);
  385. if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
  386. /*
  387. * keep this habit of first writing the "sgilabel".
  388. * I never tested whether it works without (AN 981002).
  389. */
  390. sgiinfo *info = fill_sgiinfo();
  391. int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
  392. if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
  393. fdisk_fatal(unable_to_seek);
  394. if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE)
  395. fdisk_fatal(unable_to_write);
  396. free(info);
  397. }
  398. }
  399. static int
  400. compare_start(int *x, int *y)
  401. {
  402. /*
  403. * sort according to start sectors
  404. * and prefers largest partition:
  405. * entry zero is entire disk entry
  406. */
  407. unsigned int i = *x;
  408. unsigned int j = *y;
  409. unsigned int a = sgi_get_start_sector(i);
  410. unsigned int b = sgi_get_start_sector(j);
  411. unsigned int c = sgi_get_num_sectors(i);
  412. unsigned int d = sgi_get_num_sectors(j);
  413. if (a == b)
  414. return (d > c) ? 1 : (d == c) ? 0 : -1;
  415. return (a > b) ? 1 : -1;
  416. }
  417. static int
  418. verify_sgi(int verbose)
  419. {
  420. int Index[16]; /* list of valid partitions */
  421. int sortcount = 0; /* number of used partitions, i.e. non-zero lengths */
  422. int entire = 0, i = 0;
  423. unsigned int start = 0;
  424. long long gap = 0; /* count unused blocks */
  425. unsigned int lastblock = sgi_get_lastblock();
  426. clearfreelist();
  427. for (i = 0; i < 16; i++) {
  428. if (sgi_get_num_sectors(i) != 0) {
  429. Index[sortcount++] = i;
  430. if (sgi_get_sysid(i) == SGI_ENTIRE_DISK) {
  431. if (entire++ == 1) {
  432. if (verbose)
  433. printf(_("More than one entire disk entry present.\n"));
  434. }
  435. }
  436. }
  437. }
  438. if (sortcount == 0) {
  439. if (verbose)
  440. printf(_("No partitions defined\n"));
  441. return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1;
  442. }
  443. qsort(Index, sortcount, sizeof(Index[0]), (void*)compare_start);
  444. if (sgi_get_sysid(Index[0]) == SGI_ENTIRE_DISK) {
  445. if ((Index[0] != 10) && verbose)
  446. printf(_("IRIX likes when Partition 11 covers the entire disk.\n"));
  447. if ((sgi_get_start_sector(Index[0]) != 0) && verbose)
  448. printf(_("The entire disk partition should start "
  449. "at block 0,\n"
  450. "not at diskblock %d.\n"),
  451. sgi_get_start_sector(Index[0]));
  452. if (debug) /* I do not understand how some disks fulfil it */
  453. if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose)
  454. printf(_("The entire disk partition is only %d diskblock large,\n"
  455. "but the disk is %d diskblocks long.\n"),
  456. sgi_get_num_sectors(Index[0]), lastblock);
  457. lastblock = sgi_get_num_sectors(Index[0]);
  458. } else {
  459. if (verbose)
  460. printf(_("One Partition (#11) should cover the entire disk.\n"));
  461. if (debug > 2)
  462. printf("sysid=%d\tpartition=%d\n",
  463. sgi_get_sysid(Index[0]), Index[0]+1);
  464. }
  465. for (i = 1, start = 0; i < sortcount; i++) {
  466. int cylsize = sgi_get_nsect() * sgi_get_ntrks();
  467. if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) {
  468. if (debug) /* I do not understand how some disks fulfil it */
  469. if (verbose)
  470. printf(_("Partition %d does not start on cylinder boundary.\n"),
  471. Index[i]+1);
  472. }
  473. if (sgi_get_num_sectors(Index[i]) % cylsize != 0) {
  474. if (debug) /* I do not understand how some disks fulfil it */
  475. if (verbose)
  476. printf(_("Partition %d does not end on cylinder boundary.\n"),
  477. Index[i]+1);
  478. }
  479. /* We cannot handle several "entire disk" entries. */
  480. if (sgi_get_sysid(Index[i]) == SGI_ENTIRE_DISK) continue;
  481. if (start > sgi_get_start_sector(Index[i])) {
  482. if (verbose)
  483. printf(_("The Partition %d and %d overlap by %d sectors.\n"),
  484. Index[i-1]+1, Index[i]+1,
  485. start - sgi_get_start_sector(Index[i]));
  486. if (gap > 0) gap = -gap;
  487. if (gap == 0) gap = -1;
  488. }
  489. if (start < sgi_get_start_sector(Index[i])) {
  490. if (verbose)
  491. printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
  492. sgi_get_start_sector(Index[i]) - start,
  493. start, sgi_get_start_sector(Index[i])-1);
  494. gap += sgi_get_start_sector(Index[i]) - start;
  495. add2freelist(start, sgi_get_start_sector(Index[i]));
  496. }
  497. start = sgi_get_start_sector(Index[i])
  498. + sgi_get_num_sectors(Index[i]);
  499. if (debug > 1) {
  500. if (verbose)
  501. printf("%2d:%12d\t%12d\t%12d\n", Index[i],
  502. sgi_get_start_sector(Index[i]),
  503. sgi_get_num_sectors(Index[i]),
  504. sgi_get_sysid(Index[i]));
  505. }
  506. }
  507. if (start < lastblock) {
  508. if (verbose)
  509. printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
  510. lastblock - start, start, lastblock-1);
  511. gap += lastblock - start;
  512. add2freelist(start, lastblock);
  513. }
  514. /*
  515. * Done with arithmetics
  516. * Go for details now
  517. */
  518. if (verbose) {
  519. if (!sgi_get_num_sectors(sgi_get_bootpartition())) {
  520. printf(_("\nThe boot partition does not exist.\n"));
  521. }
  522. if (!sgi_get_num_sectors(sgi_get_swappartition())) {
  523. printf(_("\nThe swap partition does not exist.\n"));
  524. } else {
  525. if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP)
  526. && (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP))
  527. printf(_("\nThe swap partition has no swap type.\n"));
  528. }
  529. if (sgi_check_bootfile("/unix"))
  530. printf(_("\tYou have chosen an unusual boot file name.\n"));
  531. }
  532. return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
  533. }
  534. static int
  535. sgi_gaps(void)
  536. {
  537. /*
  538. * returned value is:
  539. * = 0 : disk is properly filled to the rim
  540. * < 0 : there is an overlap
  541. * > 0 : there is still some vacant space
  542. */
  543. return verify_sgi(0);
  544. }
  545. static void
  546. sgi_change_sysid(int i, int sys)
  547. {
  548. if( sgi_get_num_sectors(i) == 0 ) { /* caught already before, ... */
  549. printf(_("Sorry You may change the Tag of non-empty partitions.\n"));
  550. return;
  551. }
  552. if (((sys != SGI_ENTIRE_DISK) && (sys != SGI_VOLHDR))
  553. && (sgi_get_start_sector(i) < 1) ) {
  554. read_maybe_empty(
  555. _("It is highly recommended that the partition at offset 0\n"
  556. "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
  557. "retrieve from its directory standalone tools like sash and fx.\n"
  558. "Only the \"SGI volume\" entire disk section may violate this.\n"
  559. "Type YES if you are sure about tagging this partition differently.\n"));
  560. if (strcmp(line_ptr, _("YES\n")))
  561. return;
  562. }
  563. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  564. }
  565. /* returns partition index of first entry marked as entire disk */
  566. static int
  567. sgi_entire(void)
  568. {
  569. int i;
  570. for (i = 0; i < 16; i++)
  571. if (sgi_get_sysid(i) == SGI_VOLUME)
  572. return i;
  573. return -1;
  574. }
  575. static void
  576. sgi_set_partition(int i, unsigned int start, unsigned int length, int sys)
  577. {
  578. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  579. sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length);
  580. sgilabel->partitions[i].start_sector = SGI_SSWAP32(start);
  581. set_changed(i);
  582. if (sgi_gaps() < 0) /* rebuild freelist */
  583. printf(_("Do You know, You got a partition overlap on the disk?\n"));
  584. }
  585. static void
  586. sgi_set_entire(void)
  587. {
  588. int n;
  589. for (n = 10; n < partitions; n++) {
  590. if(!sgi_get_num_sectors(n) ) {
  591. sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME);
  592. break;
  593. }
  594. }
  595. }
  596. static void
  597. sgi_set_volhdr(void)
  598. {
  599. int n;
  600. for (n = 8; n < partitions; n++) {
  601. if (!sgi_get_num_sectors(n)) {
  602. /*
  603. * 5 cylinders is an arbitrary value I like
  604. * IRIX 5.3 stored files in the volume header
  605. * (like sash, symmon, fx, ide) with ca. 3200
  606. * sectors.
  607. */
  608. if (heads * sectors * 5 < sgi_get_lastblock())
  609. sgi_set_partition(n, 0, heads * sectors * 5, SGI_VOLHDR);
  610. break;
  611. }
  612. }
  613. }
  614. static void
  615. sgi_delete_partition(int i)
  616. {
  617. sgi_set_partition(i, 0, 0, 0);
  618. }
  619. static void
  620. sgi_add_partition(int n, int sys)
  621. {
  622. char mesg[256];
  623. unsigned int first = 0, last = 0;
  624. if (n == 10) {
  625. sys = SGI_VOLUME;
  626. } else if (n == 8) {
  627. sys = 0;
  628. }
  629. if(sgi_get_num_sectors(n)) {
  630. printf(_("Partition %d is already defined. Delete "
  631. "it before re-adding it.\n"), n + 1);
  632. return;
  633. }
  634. if ((sgi_entire() == -1) && (sys != SGI_VOLUME)) {
  635. printf(_("Attempting to generate entire disk entry automatically.\n"));
  636. sgi_set_entire();
  637. sgi_set_volhdr();
  638. }
  639. if ((sgi_gaps() == 0) && (sys != SGI_VOLUME)) {
  640. printf(_("The entire disk is already covered with partitions.\n"));
  641. return;
  642. }
  643. if (sgi_gaps() < 0) {
  644. printf(_("You got a partition overlap on the disk. Fix it first!\n"));
  645. return;
  646. }
  647. snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
  648. while (1) {
  649. if(sys == SGI_VOLUME) {
  650. last = sgi_get_lastblock();
  651. first = read_int(0, 0, last-1, 0, mesg);
  652. if (first != 0) {
  653. printf(_("It is highly recommended that eleventh partition\n"
  654. "covers the entire disk and is of type 'SGI volume'\n"));
  655. }
  656. } else {
  657. first = freelist[0].first;
  658. last = freelist[0].last;
  659. first = read_int(scround(first), scround(first), scround(last)-1,
  660. 0, mesg);
  661. }
  662. if (display_in_cyl_units)
  663. first *= units_per_sector;
  664. else
  665. first = first; /* align to cylinder if you know how ... */
  666. if(!last )
  667. last = isinfreelist(first);
  668. if(last == 0) {
  669. printf(_("You will get a partition overlap on the disk. "
  670. "Fix it first!\n"));
  671. } else
  672. break;
  673. }
  674. snprintf(mesg, sizeof(mesg), _(" Last %s"), str_units(SINGULAR));
  675. last = read_int(scround(first), scround(last)-1, scround(last)-1,
  676. scround(first), mesg)+1;
  677. if (display_in_cyl_units)
  678. last *= units_per_sector;
  679. else
  680. last = last; /* align to cylinder if You know how ... */
  681. if ( (sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock() ) )
  682. printf(_("It is highly recommended that eleventh partition\n"
  683. "covers the entire disk and is of type 'SGI volume'\n"));
  684. sgi_set_partition(n, first, last-first, sys);
  685. }
  686. #ifdef CONFIG_FEATURE_FDISK_ADVANCED
  687. static void
  688. create_sgilabel(void)
  689. {
  690. struct hd_geometry geometry;
  691. struct {
  692. unsigned int start;
  693. unsigned int nsect;
  694. int sysid;
  695. } old[4];
  696. int i = 0;
  697. long longsectors; /* the number of sectors on the device */
  698. int res; /* the result from the ioctl */
  699. int sec_fac; /* the sector factor */
  700. sec_fac = sector_size / 512; /* determine the sector factor */
  701. fprintf( stderr,
  702. _("Building a new SGI disklabel. Changes will remain in memory only,\n"
  703. "until you decide to write them. After that, of course, the previous\n"
  704. "content will be unrecoverably lost.\n\n"));
  705. sgi_other_endian = (BYTE_ORDER == LITTLE_ENDIAN);
  706. res = ioctl(fd, BLKGETSIZE, &longsectors);
  707. if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
  708. heads = geometry.heads;
  709. sectors = geometry.sectors;
  710. if (res == 0) {
  711. /* the get device size ioctl was successful */
  712. cylinders = longsectors / (heads * sectors);
  713. cylinders /= sec_fac;
  714. } else {
  715. /* otherwise print error and use truncated version */
  716. cylinders = geometry.cylinders;
  717. fprintf(stderr,
  718. _("Warning: BLKGETSIZE ioctl failed on %s. "
  719. "Using geometry cylinder value of %d.\n"
  720. "This value may be truncated for devices"
  721. " > 33.8 GB.\n"), disk_device, cylinders);
  722. }
  723. }
  724. for (i = 0; i < 4; i++) {
  725. old[i].sysid = 0;
  726. if (valid_part_table_flag(MBRbuffer)) {
  727. if(get_part_table(i)->sys_ind) {
  728. old[i].sysid = get_part_table(i)->sys_ind;
  729. old[i].start = get_start_sect(get_part_table(i));
  730. old[i].nsect = get_nr_sects(get_part_table(i));
  731. printf(_("Trying to keep parameters of partition %d.\n"), i);
  732. if (debug)
  733. printf(_("ID=%02x\tSTART=%d\tLENGTH=%d\n"),
  734. old[i].sysid, old[i].start, old[i].nsect);
  735. }
  736. }
  737. }
  738. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  739. sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC);
  740. sgilabel->boot_part = SGI_SSWAP16(0);
  741. sgilabel->swap_part = SGI_SSWAP16(1);
  742. /* sizeof(sgilabel->boot_file) = 16 > 6 */
  743. memset(sgilabel->boot_file, 0, 16);
  744. strcpy((char*)sgilabel->boot_file, "/unix");
  745. sgilabel->devparam.skew = (0);
  746. sgilabel->devparam.gap1 = (0);
  747. sgilabel->devparam.gap2 = (0);
  748. sgilabel->devparam.sparecyl = (0);
  749. sgilabel->devparam.pcylcount = SGI_SSWAP16(geometry.cylinders);
  750. sgilabel->devparam.head_vol0 = SGI_SSWAP16(0);
  751. sgilabel->devparam.ntrks = SGI_SSWAP16(geometry.heads);
  752. /* tracks/cylinder (heads) */
  753. sgilabel->devparam.cmd_tag_queue_depth = (0);
  754. sgilabel->devparam.unused0 = (0);
  755. sgilabel->devparam.unused1 = SGI_SSWAP16(0);
  756. sgilabel->devparam.nsect = SGI_SSWAP16(geometry.sectors);
  757. /* sectors/track */
  758. sgilabel->devparam.bytes = SGI_SSWAP16(512);
  759. sgilabel->devparam.ilfact = SGI_SSWAP16(1);
  760. sgilabel->devparam.flags = SGI_SSWAP32(TRACK_FWD|
  761. IGNORE_ERRORS|RESEEK);
  762. sgilabel->devparam.datarate = SGI_SSWAP32(0);
  763. sgilabel->devparam.retries_on_error = SGI_SSWAP32(1);
  764. sgilabel->devparam.ms_per_word = SGI_SSWAP32(0);
  765. sgilabel->devparam.xylogics_gap1 = SGI_SSWAP16(0);
  766. sgilabel->devparam.xylogics_syncdelay = SGI_SSWAP16(0);
  767. sgilabel->devparam.xylogics_readdelay = SGI_SSWAP16(0);
  768. sgilabel->devparam.xylogics_gap2 = SGI_SSWAP16(0);
  769. sgilabel->devparam.xylogics_readgate = SGI_SSWAP16(0);
  770. sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0);
  771. memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 );
  772. memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partinfo)*16 );
  773. current_label_type = label_sgi;
  774. partitions = 16;
  775. sgi_volumes = 15;
  776. sgi_set_entire();
  777. sgi_set_volhdr();
  778. for (i = 0; i < 4; i++) {
  779. if(old[i].sysid) {
  780. sgi_set_partition(i, old[i].start, old[i].nsect, old[i].sysid);
  781. }
  782. }
  783. }
  784. static void
  785. sgi_set_xcyl(void)
  786. {
  787. /* do nothing in the beginning */
  788. }
  789. #endif /* CONFIG_FEATURE_FDISK_ADVANCED */
  790. /* _____________________________________________________________
  791. */
  792. static sgiinfo *
  793. fill_sgiinfo(void)
  794. {
  795. sgiinfo *info = calloc(1, sizeof(sgiinfo));
  796. info->magic = SGI_SSWAP32(SGI_INFO_MAGIC);
  797. info->b1 = SGI_SSWAP32(-1);
  798. info->b2 = SGI_SSWAP16(-1);
  799. info->b3 = SGI_SSWAP16(1);
  800. /* You may want to replace this string !!!!!!! */
  801. strcpy( (char*)info->scsi_string, "IBM OEM 0662S12 3 30" );
  802. strcpy( (char*)info->serial, "0000" );
  803. info->check1816 = SGI_SSWAP16(18*256 +16 );
  804. strcpy( (char*)info->installer, "Sfx version 5.3, Oct 18, 1994" );
  805. return info;
  806. }
  807. #endif /* SGI_LABEL */