fdisk_sgi.c 24 KB

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