fdisk_sgi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * Copyright (C) Andreas Neuper, Sep 1998.
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  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 = %ss 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(), units_per_sector);
  267. } else {
  268. printf("\nDisk %s (SGI disk label): "
  269. "%u heads, %u sectors, %u cylinders\n"
  270. "Units = %ss of %u * 512 bytes\n\n",
  271. disk_device, g_heads, g_sectors, g_cylinders,
  272. str_units(), 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. memcpy((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 (is_prefixed_with((char*)sgilabel->directory[0].vol_file_name, "sgilabel")) {
  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. }
  456. if (SGI_DEBUG) { /* I do not understand how some disks fulfil it */
  457. if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose)
  458. printf("The entire disk partition is only %u diskblock large,\n"
  459. "but the disk is %u diskblocks long\n",
  460. sgi_get_num_sectors(Index[0]), lastblock);
  461. lastblock = sgi_get_num_sectors(Index[0]);
  462. }
  463. } else {
  464. if (verbose)
  465. printf("One Partition (#11) should cover the entire disk\n");
  466. if (SGI_DEBUG > 2)
  467. printf("sysid=%u\tpartition=%u\n",
  468. sgi_get_sysid(Index[0]), Index[0]+1);
  469. }
  470. for (i = 1, start = 0; i < sortcount; i++) {
  471. int cylsize = sgi_get_nsect() * sgi_get_ntrks();
  472. if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) {
  473. if (SGI_DEBUG) /* I do not understand how some disks fulfil it */
  474. if (verbose)
  475. printf("Partition %u does not start on cylinder boundary\n",
  476. Index[i]+1);
  477. }
  478. if (sgi_get_num_sectors(Index[i]) % cylsize != 0) {
  479. if (SGI_DEBUG) /* I do not understand how some disks fulfil it */
  480. if (verbose)
  481. printf("Partition %u does not end on cylinder boundary\n",
  482. Index[i]+1);
  483. }
  484. /* We cannot handle several "entire disk" entries. */
  485. if (sgi_get_sysid(Index[i]) == SGI_ENTIRE_DISK) continue;
  486. if (start > sgi_get_start_sector(Index[i])) {
  487. if (verbose)
  488. printf("Partitions %u and %u overlap by %u sectors\n",
  489. Index[i-1]+1, Index[i]+1,
  490. start - sgi_get_start_sector(Index[i]));
  491. if (gap > 0) gap = -gap;
  492. if (gap == 0) gap = -1;
  493. }
  494. if (start < sgi_get_start_sector(Index[i])) {
  495. if (verbose)
  496. printf("Unused gap of %u sectors - sectors %u-%u\n",
  497. sgi_get_start_sector(Index[i]) - start,
  498. start, sgi_get_start_sector(Index[i])-1);
  499. gap += sgi_get_start_sector(Index[i]) - start;
  500. add2freelist(start, sgi_get_start_sector(Index[i]));
  501. }
  502. start = sgi_get_start_sector(Index[i])
  503. + sgi_get_num_sectors(Index[i]);
  504. if (SGI_DEBUG > 1) {
  505. if (verbose)
  506. printf("%2u:%12u\t%12u\t%12u\n", Index[i],
  507. sgi_get_start_sector(Index[i]),
  508. sgi_get_num_sectors(Index[i]),
  509. sgi_get_sysid(Index[i]));
  510. }
  511. }
  512. if (start < lastblock) {
  513. if (verbose)
  514. printf("Unused gap of %u sectors - sectors %u-%u\n",
  515. lastblock - start, start, lastblock-1);
  516. gap += lastblock - start;
  517. add2freelist(start, lastblock);
  518. }
  519. /*
  520. * Done with arithmetics
  521. * Go for details now
  522. */
  523. if (verbose) {
  524. if (!sgi_get_num_sectors(sgi_get_bootpartition())) {
  525. printf("\nThe boot partition does not exist\n");
  526. }
  527. if (!sgi_get_num_sectors(sgi_get_swappartition())) {
  528. printf("\nThe swap partition does not exist\n");
  529. } else {
  530. if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP)
  531. && (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP))
  532. printf("\nThe swap partition has no swap type\n");
  533. }
  534. if (sgi_check_bootfile("/unix"))
  535. printf("\tYou have chosen an unusual boot file name\n");
  536. }
  537. return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
  538. }
  539. static int
  540. sgi_gaps(void)
  541. {
  542. /*
  543. * returned value is:
  544. * = 0 : disk is properly filled to the rim
  545. * < 0 : there is an overlap
  546. * > 0 : there is still some vacant space
  547. */
  548. return verify_sgi(0);
  549. }
  550. static void
  551. sgi_change_sysid(int i, int sys)
  552. {
  553. if (sgi_get_num_sectors(i) == 0) { /* caught already before, ... */
  554. printf("Sorry you may change the Tag of non-empty partitions\n");
  555. return;
  556. }
  557. if ((sys != SGI_ENTIRE_DISK) && (sys != SGI_VOLHDR)
  558. && (sgi_get_start_sector(i) < 1)
  559. ) {
  560. read_maybe_empty(
  561. "It is highly recommended that the partition at offset 0\n"
  562. "is of type \"SGI volhdr\", the IRIX system will rely on it to\n"
  563. "retrieve from its directory standalone tools like sash and fx.\n"
  564. "Only the \"SGI volume\" entire disk section may violate this.\n"
  565. "Type YES if you are sure about tagging this partition differently.\n");
  566. if (strcmp(line_ptr, "YES") != 0)
  567. return;
  568. }
  569. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  570. }
  571. /* returns partition index of first entry marked as entire disk */
  572. static int
  573. sgi_entire(void)
  574. {
  575. int i;
  576. for (i = 0; i < 16; i++)
  577. if (sgi_get_sysid(i) == SGI_VOLUME)
  578. return i;
  579. return -1;
  580. }
  581. static void
  582. sgi_set_partition(int i, unsigned int start, unsigned int length, int sys)
  583. {
  584. sgilabel->partitions[i].id = SGI_SSWAP32(sys);
  585. sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length);
  586. sgilabel->partitions[i].start_sector = SGI_SSWAP32(start);
  587. set_changed(i);
  588. if (sgi_gaps() < 0) /* rebuild freelist */
  589. printf("Partition overlap detected\n");
  590. }
  591. static void
  592. sgi_set_entire(void)
  593. {
  594. int n;
  595. for (n = 10; n < g_partitions; n++) {
  596. if (!sgi_get_num_sectors(n) ) {
  597. sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME);
  598. break;
  599. }
  600. }
  601. }
  602. static void
  603. sgi_set_volhdr(void)
  604. {
  605. int n;
  606. for (n = 8; n < g_partitions; n++) {
  607. if (!sgi_get_num_sectors(n)) {
  608. /*
  609. * 5 cylinders is an arbitrary value I like
  610. * IRIX 5.3 stored files in the volume header
  611. * (like sash, symmon, fx, ide) with ca. 3200
  612. * sectors.
  613. */
  614. if (g_heads * g_sectors * 5 < sgi_get_lastblock()) {
  615. sgi_set_partition(n, 0, g_heads * g_sectors * 5, SGI_VOLHDR);
  616. break;
  617. }
  618. }
  619. }
  620. }
  621. static void
  622. sgi_delete_partition(int i)
  623. {
  624. sgi_set_partition(i, 0, 0, 0);
  625. }
  626. static void
  627. sgi_add_partition(int n, int sys)
  628. {
  629. char mesg[256];
  630. unsigned int first = 0, last = 0;
  631. if (n == 10) {
  632. sys = SGI_VOLUME;
  633. } else if (n == 8) {
  634. sys = 0;
  635. }
  636. if (sgi_get_num_sectors(n)) {
  637. printf(msg_part_already_defined, n + 1);
  638. return;
  639. }
  640. if ((sgi_entire() == -1) && (sys != SGI_VOLUME)) {
  641. printf("Attempting to generate entire disk entry automatically\n");
  642. sgi_set_entire();
  643. sgi_set_volhdr();
  644. }
  645. if ((sgi_gaps() == 0) && (sys != SGI_VOLUME)) {
  646. printf("The entire disk is already covered with partitions\n");
  647. return;
  648. }
  649. if (sgi_gaps() < 0) {
  650. printf("You got a partition overlap on the disk. Fix it first!\n");
  651. return;
  652. }
  653. snprintf(mesg, sizeof(mesg), "First %s", str_units());
  654. while (1) {
  655. if (sys == SGI_VOLUME) {
  656. last = sgi_get_lastblock();
  657. first = read_int(0, 0, last-1, 0, mesg);
  658. if (first != 0) {
  659. printf("It is highly recommended that eleventh partition\n"
  660. "covers the entire disk and is of type 'SGI volume'\n");
  661. }
  662. } else {
  663. first = freelist[0].first;
  664. last = freelist[0].last;
  665. first = read_int(scround(first), scround(first), scround(last)-1,
  666. 0, mesg);
  667. }
  668. if (display_in_cyl_units)
  669. first *= units_per_sector;
  670. else
  671. first = first; /* align to cylinder if you know how ... */
  672. if (!last )
  673. last = isinfreelist(first);
  674. if (last != 0)
  675. break;
  676. printf("You will get a partition overlap on the disk. "
  677. "Fix it first!\n");
  678. }
  679. snprintf(mesg, sizeof(mesg), " Last %s", str_units());
  680. last = read_int(scround(first), scround(last)-1, scround(last)-1,
  681. scround(first), mesg)+1;
  682. if (display_in_cyl_units)
  683. last *= units_per_sector;
  684. else
  685. last = last; /* align to cylinder if You know how ... */
  686. if ( (sys == SGI_VOLUME) && (first != 0 || last != sgi_get_lastblock() ) )
  687. printf("It is highly recommended that eleventh partition\n"
  688. "covers the entire disk and is of type 'SGI volume'\n");
  689. sgi_set_partition(n, first, last-first, sys);
  690. }
  691. #if ENABLE_FEATURE_FDISK_ADVANCED
  692. static void
  693. create_sgilabel(void)
  694. {
  695. struct hd_geometry geometry;
  696. struct {
  697. unsigned int start;
  698. unsigned int nsect;
  699. int sysid;
  700. } old[4];
  701. int i = 0;
  702. long longsectors; /* the number of sectors on the device */
  703. int res; /* the result from the ioctl */
  704. int sec_fac; /* the sector factor */
  705. sec_fac = sector_size / 512; /* determine the sector factor */
  706. printf(msg_building_new_label, "SGI disklabel");
  707. sgi_other_endian = BB_LITTLE_ENDIAN;
  708. res = ioctl(dev_fd, BLKGETSIZE, &longsectors);
  709. if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
  710. g_heads = geometry.heads;
  711. g_sectors = geometry.sectors;
  712. if (res == 0) {
  713. /* the get device size ioctl was successful */
  714. g_cylinders = longsectors / (g_heads * g_sectors);
  715. g_cylinders /= sec_fac;
  716. } else {
  717. /* otherwise print error and use truncated version */
  718. g_cylinders = geometry.cylinders;
  719. printf(
  720. "Warning: BLKGETSIZE ioctl failed on %s. Using geometry cylinder value of %u.\n"
  721. "This value may be truncated for devices > 33.8 GB.\n", disk_device, g_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 %u\n", i);
  732. if (SGI_DEBUG)
  733. printf("ID=%02x\tSTART=%u\tLENGTH=%u\n",
  734. old[i].sysid, old[i].start, old[i].nsect);
  735. }
  736. }
  737. }
  738. memset(MBRbuffer, 0, sizeof(MBRbuffer));
  739. /* fields with '//' are already zeroed out by memset above */
  740. sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC);
  741. //sgilabel->boot_part = SGI_SSWAP16(0);
  742. sgilabel->swap_part = SGI_SSWAP16(1);
  743. //memset(sgilabel->boot_file, 0, 16);
  744. strcpy((char*)sgilabel->boot_file, "/unix"); /* sizeof(sgilabel->boot_file) == 16 > 6 */
  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. /* tracks/cylinder (heads) */
  752. sgilabel->devparam.ntrks = SGI_SSWAP16(geometry.heads);
  753. //sgilabel->devparam.cmd_tag_queue_depth = (0);
  754. //sgilabel->devparam.unused0 = (0);
  755. //sgilabel->devparam.unused1 = SGI_SSWAP16(0);
  756. /* sectors/track */
  757. sgilabel->devparam.nsect = SGI_SSWAP16(geometry.sectors);
  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. g_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 /* FEATURE_FDISK_ADVANCED */
  790. /* _____________________________________________________________
  791. */
  792. static sgiinfo *
  793. fill_sgiinfo(void)
  794. {
  795. sgiinfo *info = xzalloc(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 */