fdisk.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  1. /* vi: set sw=4 ts=4: */
  2. /* fdisk.c -- Partition table manipulator for Linux.
  3. *
  4. * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
  5. * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #ifndef _LARGEFILE64_SOURCE
  10. /* For lseek64 */
  11. # define _LARGEFILE64_SOURCE
  12. #endif
  13. #include <assert.h> /* assert */
  14. #include <sys/mount.h>
  15. #if !defined(BLKSSZGET)
  16. # define BLKSSZGET _IO(0x12, 104)
  17. #endif
  18. #if !defined(BLKGETSIZE64)
  19. # define BLKGETSIZE64 _IOR(0x12,114,size_t)
  20. #endif
  21. #include "libbb.h"
  22. #if BB_LITTLE_ENDIAN
  23. # define inline_if_little_endian ALWAYS_INLINE
  24. #else
  25. # define inline_if_little_endian /* nothing */
  26. #endif
  27. /* Looks like someone forgot to add this to config system */
  28. #ifndef ENABLE_FEATURE_FDISK_BLKSIZE
  29. # define ENABLE_FEATURE_FDISK_BLKSIZE 0
  30. # define IF_FEATURE_FDISK_BLKSIZE(a)
  31. #endif
  32. #define DEFAULT_SECTOR_SIZE 512
  33. #define DEFAULT_SECTOR_SIZE_STR "512"
  34. #define MAX_SECTOR_SIZE 2048
  35. #define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
  36. #define MAXIMUM_PARTS 60
  37. #define ACTIVE_FLAG 0x80
  38. #define EXTENDED 0x05
  39. #define WIN98_EXTENDED 0x0f
  40. #define LINUX_PARTITION 0x81
  41. #define LINUX_SWAP 0x82
  42. #define LINUX_NATIVE 0x83
  43. #define LINUX_EXTENDED 0x85
  44. #define LINUX_LVM 0x8e
  45. #define LINUX_RAID 0xfd
  46. enum {
  47. OPT_b = 1 << 0,
  48. OPT_C = 1 << 1,
  49. OPT_H = 1 << 2,
  50. OPT_l = 1 << 3,
  51. OPT_S = 1 << 4,
  52. OPT_u = 1 << 5,
  53. OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
  54. };
  55. typedef unsigned long long ullong;
  56. /* Used for sector numbers. Partition formats we know
  57. * do not support more than 2^32 sectors
  58. */
  59. typedef uint32_t sector_t;
  60. #if UINT_MAX == 4294967295
  61. # define SECT_FMT ""
  62. #elif ULONG_MAX == 4294967295
  63. # define SECT_FMT "l"
  64. #else
  65. # error Cant detect sizeof(uint32_t)
  66. #endif
  67. struct hd_geometry {
  68. unsigned char heads;
  69. unsigned char sectors;
  70. unsigned short cylinders;
  71. unsigned long start;
  72. };
  73. #define HDIO_GETGEO 0x0301 /* get device geometry */
  74. static const char msg_building_new_label[] ALIGN1 =
  75. "Building a new %s. Changes will remain in memory only,\n"
  76. "until you decide to write them. After that the previous content\n"
  77. "won't be recoverable.\n\n";
  78. static const char msg_part_already_defined[] ALIGN1 =
  79. "Partition %u is already defined, delete it before re-adding\n";
  80. struct partition {
  81. unsigned char boot_ind; /* 0x80 - active */
  82. unsigned char head; /* starting head */
  83. unsigned char sector; /* starting sector */
  84. unsigned char cyl; /* starting cylinder */
  85. unsigned char sys_ind; /* what partition type */
  86. unsigned char end_head; /* end head */
  87. unsigned char end_sector; /* end sector */
  88. unsigned char end_cyl; /* end cylinder */
  89. unsigned char start4[4]; /* starting sector counting from 0 */
  90. unsigned char size4[4]; /* nr of sectors in partition */
  91. } PACKED;
  92. static const char unable_to_open[] ALIGN1 = "can't open '%s'";
  93. static const char unable_to_read[] ALIGN1 = "can't read from %s";
  94. static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
  95. enum label_type {
  96. LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
  97. };
  98. #define LABEL_IS_DOS (LABEL_DOS == current_label_type)
  99. #if ENABLE_FEATURE_SUN_LABEL
  100. #define LABEL_IS_SUN (LABEL_SUN == current_label_type)
  101. #define STATIC_SUN static
  102. #else
  103. #define LABEL_IS_SUN 0
  104. #define STATIC_SUN extern
  105. #endif
  106. #if ENABLE_FEATURE_SGI_LABEL
  107. #define LABEL_IS_SGI (LABEL_SGI == current_label_type)
  108. #define STATIC_SGI static
  109. #else
  110. #define LABEL_IS_SGI 0
  111. #define STATIC_SGI extern
  112. #endif
  113. #if ENABLE_FEATURE_AIX_LABEL
  114. #define LABEL_IS_AIX (LABEL_AIX == current_label_type)
  115. #define STATIC_AIX static
  116. #else
  117. #define LABEL_IS_AIX 0
  118. #define STATIC_AIX extern
  119. #endif
  120. #if ENABLE_FEATURE_OSF_LABEL
  121. #define LABEL_IS_OSF (LABEL_OSF == current_label_type)
  122. #define STATIC_OSF static
  123. #else
  124. #define LABEL_IS_OSF 0
  125. #define STATIC_OSF extern
  126. #endif
  127. enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
  128. static void update_units(void);
  129. #if ENABLE_FEATURE_FDISK_WRITABLE
  130. static void change_units(void);
  131. static void reread_partition_table(int leave);
  132. static void delete_partition(int i);
  133. static unsigned get_partition(int warn, unsigned max);
  134. static void list_types(const char *const *sys);
  135. static sector_t read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg);
  136. #endif
  137. static const char *partition_type(unsigned char type);
  138. static void get_geometry(void);
  139. #if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
  140. static int get_boot(enum action what);
  141. #else
  142. static int get_boot(void);
  143. #endif
  144. #define PLURAL 0
  145. #define SINGULAR 1
  146. static sector_t get_start_sect(const struct partition *p);
  147. static sector_t get_nr_sects(const struct partition *p);
  148. /*
  149. * per partition table entry data
  150. *
  151. * The four primary partitions have the same sectorbuffer (MBRbuffer)
  152. * and have NULL ext_pointer.
  153. * Each logical partition table entry has two pointers, one for the
  154. * partition and one link to the next one.
  155. */
  156. struct pte {
  157. struct partition *part_table; /* points into sectorbuffer */
  158. struct partition *ext_pointer; /* points into sectorbuffer */
  159. sector_t offset_from_dev_start; /* disk sector number */
  160. char *sectorbuffer; /* disk sector contents */
  161. #if ENABLE_FEATURE_FDISK_WRITABLE
  162. char changed; /* boolean */
  163. #endif
  164. };
  165. /* DOS partition types */
  166. static const char *const i386_sys_types[] = {
  167. "\x00" "Empty",
  168. "\x01" "FAT12",
  169. "\x04" "FAT16 <32M",
  170. "\x05" "Extended", /* DOS 3.3+ extended partition */
  171. "\x06" "FAT16", /* DOS 16-bit >=32M */
  172. "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
  173. "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
  174. "\x0b" "Win95 FAT32",
  175. "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
  176. "\x0e" "Win95 FAT16 (LBA)",
  177. "\x0f" "Win95 Ext'd (LBA)",
  178. "\x11" "Hidden FAT12",
  179. "\x12" "Compaq diagnostics",
  180. "\x14" "Hidden FAT16 <32M",
  181. "\x16" "Hidden FAT16",
  182. "\x17" "Hidden HPFS/NTFS",
  183. "\x1b" "Hidden Win95 FAT32",
  184. "\x1c" "Hidden W95 FAT32 (LBA)",
  185. "\x1e" "Hidden W95 FAT16 (LBA)",
  186. "\x3c" "Part.Magic recovery",
  187. "\x41" "PPC PReP Boot",
  188. "\x42" "SFS",
  189. "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
  190. "\x80" "Old Minix", /* Minix 1.4a and earlier */
  191. "\x81" "Minix / old Linux",/* Minix 1.4b and later */
  192. "\x82" "Linux swap", /* also Solaris */
  193. "\x83" "Linux",
  194. "\x84" "OS/2 hidden C: drive",
  195. "\x85" "Linux extended",
  196. "\x86" "NTFS volume set",
  197. "\x87" "NTFS volume set",
  198. "\x8e" "Linux LVM",
  199. "\x9f" "BSD/OS", /* BSDI */
  200. "\xa0" "Thinkpad hibernation",
  201. "\xa5" "FreeBSD", /* various BSD flavours */
  202. "\xa6" "OpenBSD",
  203. "\xa8" "Darwin UFS",
  204. "\xa9" "NetBSD",
  205. "\xab" "Darwin boot",
  206. "\xb7" "BSDI fs",
  207. "\xb8" "BSDI swap",
  208. "\xbe" "Solaris boot",
  209. "\xeb" "BeOS fs",
  210. "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
  211. "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
  212. "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
  213. "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
  214. "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
  215. autodetect using persistent
  216. superblock */
  217. #if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
  218. "\x02" "XENIX root",
  219. "\x03" "XENIX usr",
  220. "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
  221. "\x09" "AIX bootable", /* AIX data or Coherent */
  222. "\x10" "OPUS",
  223. "\x18" "AST SmartSleep",
  224. "\x24" "NEC DOS",
  225. "\x39" "Plan 9",
  226. "\x40" "Venix 80286",
  227. "\x4d" "QNX4.x",
  228. "\x4e" "QNX4.x 2nd part",
  229. "\x4f" "QNX4.x 3rd part",
  230. "\x50" "OnTrack DM",
  231. "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
  232. "\x52" "CP/M", /* CP/M or Microport SysV/AT */
  233. "\x53" "OnTrack DM6 Aux3",
  234. "\x54" "OnTrackDM6",
  235. "\x55" "EZ-Drive",
  236. "\x56" "Golden Bow",
  237. "\x5c" "Priam Edisk",
  238. "\x61" "SpeedStor",
  239. "\x64" "Novell Netware 286",
  240. "\x65" "Novell Netware 386",
  241. "\x70" "DiskSecure Multi-Boot",
  242. "\x75" "PC/IX",
  243. "\x93" "Amoeba",
  244. "\x94" "Amoeba BBT", /* (bad block table) */
  245. "\xa7" "NeXTSTEP",
  246. "\xbb" "Boot Wizard hidden",
  247. "\xc1" "DRDOS/sec (FAT-12)",
  248. "\xc4" "DRDOS/sec (FAT-16 < 32M)",
  249. "\xc6" "DRDOS/sec (FAT-16)",
  250. "\xc7" "Syrinx",
  251. "\xda" "Non-FS data",
  252. "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
  253. Concurrent DOS or CTOS */
  254. "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
  255. "\xdf" "BootIt", /* BootIt EMBRM */
  256. "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
  257. extended partition */
  258. "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
  259. "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
  260. partition < 1024 cyl. */
  261. "\xf1" "SpeedStor",
  262. "\xf4" "SpeedStor", /* SpeedStor large partition */
  263. "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
  264. "\xff" "BBT", /* Xenix Bad Block Table */
  265. #endif
  266. NULL
  267. };
  268. enum {
  269. dev_fd = 3 /* the disk */
  270. };
  271. /* Globals */
  272. struct globals {
  273. char *line_ptr;
  274. const char *disk_device;
  275. int g_partitions; // = 4; /* maximum partition + 1 */
  276. unsigned units_per_sector; // = 1;
  277. unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
  278. unsigned user_set_sector_size;
  279. unsigned sector_offset; // = 1;
  280. unsigned g_heads, g_sectors, g_cylinders;
  281. smallint /* enum label_type */ current_label_type;
  282. smallint display_in_cyl_units; // = 1;
  283. #if ENABLE_FEATURE_OSF_LABEL
  284. smallint possibly_osf_label;
  285. #endif
  286. smallint listing; /* no aborts for fdisk -l */
  287. smallint dos_compatible_flag; // = 1;
  288. #if ENABLE_FEATURE_FDISK_WRITABLE
  289. //int dos_changed;
  290. smallint nowarn; /* no warnings for fdisk -l/-s */
  291. #endif
  292. int ext_index; /* the prime extended partition */
  293. unsigned user_cylinders, user_heads, user_sectors;
  294. unsigned pt_heads, pt_sectors;
  295. unsigned kern_heads, kern_sectors;
  296. sector_t extended_offset; /* offset of link pointers */
  297. sector_t total_number_of_sectors;
  298. jmp_buf listingbuf;
  299. char line_buffer[80];
  300. char partname_buffer[80];
  301. /* Raw disk label. For DOS-type partition tables the MBR,
  302. * with descriptions of the primary partitions. */
  303. char MBRbuffer[MAX_SECTOR_SIZE];
  304. /* Partition tables */
  305. struct pte ptes[MAXIMUM_PARTS];
  306. };
  307. #define G (*ptr_to_globals)
  308. #define line_ptr (G.line_ptr )
  309. #define disk_device (G.disk_device )
  310. #define g_partitions (G.g_partitions )
  311. #define units_per_sector (G.units_per_sector )
  312. #define sector_size (G.sector_size )
  313. #define user_set_sector_size (G.user_set_sector_size)
  314. #define sector_offset (G.sector_offset )
  315. #define g_heads (G.g_heads )
  316. #define g_sectors (G.g_sectors )
  317. #define g_cylinders (G.g_cylinders )
  318. #define current_label_type (G.current_label_type )
  319. #define display_in_cyl_units (G.display_in_cyl_units)
  320. #define possibly_osf_label (G.possibly_osf_label )
  321. #define listing (G.listing )
  322. #define dos_compatible_flag (G.dos_compatible_flag )
  323. #define nowarn (G.nowarn )
  324. #define ext_index (G.ext_index )
  325. #define user_cylinders (G.user_cylinders )
  326. #define user_heads (G.user_heads )
  327. #define user_sectors (G.user_sectors )
  328. #define pt_heads (G.pt_heads )
  329. #define pt_sectors (G.pt_sectors )
  330. #define kern_heads (G.kern_heads )
  331. #define kern_sectors (G.kern_sectors )
  332. #define extended_offset (G.extended_offset )
  333. #define total_number_of_sectors (G.total_number_of_sectors)
  334. #define listingbuf (G.listingbuf )
  335. #define line_buffer (G.line_buffer )
  336. #define partname_buffer (G.partname_buffer)
  337. #define MBRbuffer (G.MBRbuffer )
  338. #define ptes (G.ptes )
  339. #define INIT_G() do { \
  340. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  341. sector_size = DEFAULT_SECTOR_SIZE; \
  342. sector_offset = 1; \
  343. g_partitions = 4; \
  344. display_in_cyl_units = 1; \
  345. units_per_sector = 1; \
  346. dos_compatible_flag = 1; \
  347. } while (0)
  348. /* TODO: move to libbb? */
  349. /* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
  350. * disks > 2^32 sectors
  351. */
  352. static sector_t bb_BLKGETSIZE_sectors(int fd)
  353. {
  354. uint64_t v64;
  355. unsigned long longsectors;
  356. if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
  357. /* Got bytes, convert to 512 byte sectors */
  358. v64 >>= 9;
  359. if (v64 != (sector_t)v64) {
  360. ret_trunc:
  361. /* Not only DOS, but all other partition tables
  362. * we support can't record more than 32 bit
  363. * sector counts or offsets
  364. */
  365. bb_error_msg("device has more than 2^32 sectors, can't use all of them");
  366. v64 = (uint32_t)-1L;
  367. }
  368. return v64;
  369. }
  370. /* Needs temp of type long */
  371. if (ioctl(fd, BLKGETSIZE, &longsectors)) {
  372. /* Perhaps this is a disk image */
  373. off_t sz = lseek(fd, 0, SEEK_END);
  374. longsectors = 0;
  375. if (sz > 0)
  376. longsectors = (uoff_t)sz / sector_size;
  377. lseek(fd, 0, SEEK_SET);
  378. }
  379. if (sizeof(long) > sizeof(sector_t)
  380. && longsectors != (sector_t)longsectors
  381. ) {
  382. goto ret_trunc;
  383. }
  384. return longsectors;
  385. }
  386. #define IS_EXTENDED(i) \
  387. ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
  388. #define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
  389. #define scround(x) (((x)+units_per_sector-1)/units_per_sector)
  390. #define pt_offset(b, n) \
  391. ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
  392. #define sector(s) ((s) & 0x3f)
  393. #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
  394. #define hsc2sector(h,s,c) \
  395. (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
  396. static void
  397. close_dev_fd(void)
  398. {
  399. /* Not really closing, but making sure it is open, and to harmless place */
  400. xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
  401. }
  402. /*
  403. * Return partition name - uses static storage
  404. */
  405. static const char *
  406. partname(const char *dev, int pno, int lth)
  407. {
  408. const char *p;
  409. int w, wp;
  410. int bufsiz;
  411. char *bufp;
  412. bufp = partname_buffer;
  413. bufsiz = sizeof(partname_buffer);
  414. w = strlen(dev);
  415. p = "";
  416. if (isdigit(dev[w-1]))
  417. p = "p";
  418. /* devfs kludge - note: fdisk partition names are not supposed
  419. to equal kernel names, so there is no reason to do this */
  420. if (strcmp(dev + w - 4, "disc") == 0) {
  421. w -= 4;
  422. p = "part";
  423. }
  424. wp = strlen(p);
  425. if (lth) {
  426. snprintf(bufp, bufsiz, "%*.*s%s%-2u",
  427. lth-wp-2, w, dev, p, pno);
  428. } else {
  429. snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
  430. }
  431. return bufp;
  432. }
  433. static ALWAYS_INLINE struct partition *
  434. get_part_table(int i)
  435. {
  436. return ptes[i].part_table;
  437. }
  438. static const char *
  439. str_units(int n)
  440. { /* n==1: use singular */
  441. if (n == 1)
  442. return display_in_cyl_units ? "cylinder" : "sector";
  443. return display_in_cyl_units ? "cylinders" : "sectors";
  444. }
  445. static int
  446. valid_part_table_flag(const char *mbuffer)
  447. {
  448. return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
  449. }
  450. static void fdisk_fatal(const char *why)
  451. {
  452. if (listing) {
  453. close_dev_fd();
  454. longjmp(listingbuf, 1);
  455. }
  456. bb_error_msg_and_die(why, disk_device);
  457. }
  458. static void
  459. seek_sector(sector_t secno)
  460. {
  461. #if ENABLE_FDISK_SUPPORT_LARGE_DISKS
  462. off64_t off = (off64_t)secno * sector_size;
  463. if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
  464. fdisk_fatal(unable_to_seek);
  465. #else
  466. uint64_t off = (uint64_t)secno * sector_size;
  467. if (off > MAXINT(off_t)
  468. || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
  469. ) {
  470. fdisk_fatal(unable_to_seek);
  471. }
  472. #endif
  473. }
  474. #if ENABLE_FEATURE_FDISK_WRITABLE
  475. /* Read line; return 0 or first printable char */
  476. static int
  477. read_line(const char *prompt)
  478. {
  479. int sz;
  480. sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
  481. if (sz <= 0)
  482. exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
  483. if (line_buffer[sz-1] == '\n')
  484. line_buffer[--sz] = '\0';
  485. line_ptr = line_buffer;
  486. while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
  487. line_ptr++;
  488. return *line_ptr;
  489. }
  490. static void
  491. set_all_unchanged(void)
  492. {
  493. int i;
  494. for (i = 0; i < MAXIMUM_PARTS; i++)
  495. ptes[i].changed = 0;
  496. }
  497. static ALWAYS_INLINE void
  498. set_changed(int i)
  499. {
  500. ptes[i].changed = 1;
  501. }
  502. static ALWAYS_INLINE void
  503. write_part_table_flag(char *b)
  504. {
  505. b[510] = 0x55;
  506. b[511] = 0xaa;
  507. }
  508. static char
  509. read_nonempty(const char *mesg)
  510. {
  511. while (!read_line(mesg))
  512. continue;
  513. return *line_ptr;
  514. }
  515. static char
  516. read_maybe_empty(const char *mesg)
  517. {
  518. if (!read_line(mesg)) {
  519. line_ptr = line_buffer;
  520. line_ptr[0] = '\n';
  521. line_ptr[1] = '\0';
  522. }
  523. return line_ptr[0];
  524. }
  525. static int
  526. read_hex(const char *const *sys)
  527. {
  528. unsigned long v;
  529. while (1) {
  530. read_nonempty("Hex code (type L to list codes): ");
  531. if ((line_ptr[0] | 0x20) == 'l') {
  532. list_types(sys);
  533. continue;
  534. }
  535. v = bb_strtoul(line_ptr, NULL, 16);
  536. if (v <= 0xff)
  537. return v;
  538. }
  539. }
  540. static void
  541. write_sector(sector_t secno, const void *buf)
  542. {
  543. seek_sector(secno);
  544. xwrite(dev_fd, buf, sector_size);
  545. }
  546. #endif /* FEATURE_FDISK_WRITABLE */
  547. #include "fdisk_aix.c"
  548. struct sun_partition {
  549. unsigned char info[128]; /* Informative text string */
  550. unsigned char spare0[14];
  551. struct sun_info {
  552. unsigned char spare1;
  553. unsigned char id;
  554. unsigned char spare2;
  555. unsigned char flags;
  556. } infos[8];
  557. unsigned char spare1[246]; /* Boot information etc. */
  558. unsigned short rspeed; /* Disk rotational speed */
  559. unsigned short pcylcount; /* Physical cylinder count */
  560. unsigned short sparecyl; /* extra sects per cylinder */
  561. unsigned char spare2[4]; /* More magic... */
  562. unsigned short ilfact; /* Interleave factor */
  563. unsigned short ncyl; /* Data cylinder count */
  564. unsigned short nacyl; /* Alt. cylinder count */
  565. unsigned short ntrks; /* Tracks per cylinder */
  566. unsigned short nsect; /* Sectors per track */
  567. unsigned char spare3[4]; /* Even more magic... */
  568. struct sun_partinfo {
  569. uint32_t start_cylinder;
  570. uint32_t num_sectors;
  571. } partitions[8];
  572. unsigned short magic; /* Magic number */
  573. unsigned short csum; /* Label xor'd checksum */
  574. } FIX_ALIASING;
  575. typedef struct sun_partition sun_partition;
  576. #define sunlabel ((sun_partition *)MBRbuffer)
  577. STATIC_OSF void bsd_select(void);
  578. STATIC_OSF void xbsd_print_disklabel(int);
  579. #include "fdisk_osf.c"
  580. #if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
  581. static uint16_t
  582. fdisk_swap16(uint16_t x)
  583. {
  584. return (x << 8) | (x >> 8);
  585. }
  586. static uint32_t
  587. fdisk_swap32(uint32_t x)
  588. {
  589. return (x << 24) |
  590. ((x & 0xFF00) << 8) |
  591. ((x & 0xFF0000) >> 8) |
  592. (x >> 24);
  593. }
  594. #endif
  595. STATIC_SGI const char *const sgi_sys_types[];
  596. STATIC_SGI unsigned sgi_get_num_sectors(int i);
  597. STATIC_SGI int sgi_get_sysid(int i);
  598. STATIC_SGI void sgi_delete_partition(int i);
  599. STATIC_SGI void sgi_change_sysid(int i, int sys);
  600. STATIC_SGI void sgi_list_table(int xtra);
  601. #if ENABLE_FEATURE_FDISK_ADVANCED
  602. STATIC_SGI void sgi_set_xcyl(void);
  603. #endif
  604. STATIC_SGI int verify_sgi(int verbose);
  605. STATIC_SGI void sgi_add_partition(int n, int sys);
  606. STATIC_SGI void sgi_set_swappartition(int i);
  607. STATIC_SGI const char *sgi_get_bootfile(void);
  608. STATIC_SGI void sgi_set_bootfile(const char* aFile);
  609. STATIC_SGI void create_sgiinfo(void);
  610. STATIC_SGI void sgi_write_table(void);
  611. STATIC_SGI void sgi_set_bootpartition(int i);
  612. #include "fdisk_sgi.c"
  613. STATIC_SUN const char *const sun_sys_types[];
  614. STATIC_SUN void sun_delete_partition(int i);
  615. STATIC_SUN void sun_change_sysid(int i, int sys);
  616. STATIC_SUN void sun_list_table(int xtra);
  617. STATIC_SUN void add_sun_partition(int n, int sys);
  618. #if ENABLE_FEATURE_FDISK_ADVANCED
  619. STATIC_SUN void sun_set_alt_cyl(void);
  620. STATIC_SUN void sun_set_ncyl(int cyl);
  621. STATIC_SUN void sun_set_xcyl(void);
  622. STATIC_SUN void sun_set_ilfact(void);
  623. STATIC_SUN void sun_set_rspeed(void);
  624. STATIC_SUN void sun_set_pcylcount(void);
  625. #endif
  626. STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
  627. STATIC_SUN void verify_sun(void);
  628. STATIC_SUN void sun_write_table(void);
  629. #include "fdisk_sun.c"
  630. static inline_if_little_endian unsigned
  631. read4_little_endian(const unsigned char *cp)
  632. {
  633. uint32_t v;
  634. move_from_unaligned32(v, cp);
  635. return SWAP_LE32(v);
  636. }
  637. static sector_t
  638. get_start_sect(const struct partition *p)
  639. {
  640. return read4_little_endian(p->start4);
  641. }
  642. static sector_t
  643. get_nr_sects(const struct partition *p)
  644. {
  645. return read4_little_endian(p->size4);
  646. }
  647. #if ENABLE_FEATURE_FDISK_WRITABLE
  648. /* start_sect and nr_sects are stored little endian on all machines */
  649. /* moreover, they are not aligned correctly */
  650. static inline_if_little_endian void
  651. store4_little_endian(unsigned char *cp, unsigned val)
  652. {
  653. uint32_t v = SWAP_LE32(val);
  654. move_to_unaligned32(cp, v);
  655. }
  656. static void
  657. set_start_sect(struct partition *p, unsigned start_sect)
  658. {
  659. store4_little_endian(p->start4, start_sect);
  660. }
  661. static void
  662. set_nr_sects(struct partition *p, unsigned nr_sects)
  663. {
  664. store4_little_endian(p->size4, nr_sects);
  665. }
  666. #endif
  667. /* Allocate a buffer and read a partition table sector */
  668. static void
  669. read_pte(struct pte *pe, sector_t offset)
  670. {
  671. pe->offset_from_dev_start = offset;
  672. pe->sectorbuffer = xzalloc(sector_size);
  673. seek_sector(offset);
  674. /* xread would make us abort - bad for fdisk -l */
  675. if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
  676. fdisk_fatal(unable_to_read);
  677. #if ENABLE_FEATURE_FDISK_WRITABLE
  678. pe->changed = 0;
  679. #endif
  680. pe->part_table = pe->ext_pointer = NULL;
  681. }
  682. static sector_t
  683. get_partition_start_from_dev_start(const struct pte *pe)
  684. {
  685. return pe->offset_from_dev_start + get_start_sect(pe->part_table);
  686. }
  687. #if ENABLE_FEATURE_FDISK_WRITABLE
  688. /*
  689. * Avoid warning about DOS partitions when no DOS partition was changed.
  690. * Here a heuristic "is probably dos partition".
  691. * We might also do the opposite and warn in all cases except
  692. * for "is probably nondos partition".
  693. */
  694. #ifdef UNUSED
  695. static int
  696. is_dos_partition(int t)
  697. {
  698. return (t == 1 || t == 4 || t == 6 ||
  699. t == 0x0b || t == 0x0c || t == 0x0e ||
  700. t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
  701. t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
  702. t == 0xc1 || t == 0xc4 || t == 0xc6);
  703. }
  704. #endif
  705. static void
  706. menu(void)
  707. {
  708. puts("Command Action");
  709. if (LABEL_IS_SUN) {
  710. puts("a\ttoggle a read only flag"); /* sun */
  711. puts("b\tedit bsd disklabel");
  712. puts("c\ttoggle the mountable flag"); /* sun */
  713. puts("d\tdelete a partition");
  714. puts("l\tlist known partition types");
  715. puts("n\tadd a new partition");
  716. puts("o\tcreate a new empty DOS partition table");
  717. puts("p\tprint the partition table");
  718. puts("q\tquit without saving changes");
  719. puts("s\tcreate a new empty Sun disklabel"); /* sun */
  720. puts("t\tchange a partition's system id");
  721. puts("u\tchange display/entry units");
  722. puts("v\tverify the partition table");
  723. puts("w\twrite table to disk and exit");
  724. #if ENABLE_FEATURE_FDISK_ADVANCED
  725. puts("x\textra functionality (experts only)");
  726. #endif
  727. } else if (LABEL_IS_SGI) {
  728. puts("a\tselect bootable partition"); /* sgi flavour */
  729. puts("b\tedit bootfile entry"); /* sgi */
  730. puts("c\tselect sgi swap partition"); /* sgi flavour */
  731. puts("d\tdelete a partition");
  732. puts("l\tlist known partition types");
  733. puts("n\tadd a new partition");
  734. puts("o\tcreate a new empty DOS partition table");
  735. puts("p\tprint the partition table");
  736. puts("q\tquit without saving changes");
  737. puts("s\tcreate a new empty Sun disklabel"); /* sun */
  738. puts("t\tchange a partition's system id");
  739. puts("u\tchange display/entry units");
  740. puts("v\tverify the partition table");
  741. puts("w\twrite table to disk and exit");
  742. } else if (LABEL_IS_AIX) {
  743. puts("o\tcreate a new empty DOS partition table");
  744. puts("q\tquit without saving changes");
  745. puts("s\tcreate a new empty Sun disklabel"); /* sun */
  746. } else {
  747. puts("a\ttoggle a bootable flag");
  748. puts("b\tedit bsd disklabel");
  749. puts("c\ttoggle the dos compatibility flag");
  750. puts("d\tdelete a partition");
  751. puts("l\tlist known partition types");
  752. puts("n\tadd a new partition");
  753. puts("o\tcreate a new empty DOS partition table");
  754. puts("p\tprint the partition table");
  755. puts("q\tquit without saving changes");
  756. puts("s\tcreate a new empty Sun disklabel"); /* sun */
  757. puts("t\tchange a partition's system id");
  758. puts("u\tchange display/entry units");
  759. puts("v\tverify the partition table");
  760. puts("w\twrite table to disk and exit");
  761. #if ENABLE_FEATURE_FDISK_ADVANCED
  762. puts("x\textra functionality (experts only)");
  763. #endif
  764. }
  765. }
  766. #endif /* FEATURE_FDISK_WRITABLE */
  767. #if ENABLE_FEATURE_FDISK_ADVANCED
  768. static void
  769. xmenu(void)
  770. {
  771. puts("Command Action");
  772. if (LABEL_IS_SUN) {
  773. puts("a\tchange number of alternate cylinders"); /*sun*/
  774. puts("c\tchange number of cylinders");
  775. puts("d\tprint the raw data in the partition table");
  776. puts("e\tchange number of extra sectors per cylinder");/*sun*/
  777. puts("h\tchange number of heads");
  778. puts("i\tchange interleave factor"); /*sun*/
  779. puts("o\tchange rotation speed (rpm)"); /*sun*/
  780. puts("p\tprint the partition table");
  781. puts("q\tquit without saving changes");
  782. puts("r\treturn to main menu");
  783. puts("s\tchange number of sectors/track");
  784. puts("v\tverify the partition table");
  785. puts("w\twrite table to disk and exit");
  786. puts("y\tchange number of physical cylinders"); /*sun*/
  787. } else if (LABEL_IS_SGI) {
  788. puts("b\tmove beginning of data in a partition"); /* !sun */
  789. puts("c\tchange number of cylinders");
  790. puts("d\tprint the raw data in the partition table");
  791. puts("e\tlist extended partitions"); /* !sun */
  792. puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
  793. puts("h\tchange number of heads");
  794. puts("p\tprint the partition table");
  795. puts("q\tquit without saving changes");
  796. puts("r\treturn to main menu");
  797. puts("s\tchange number of sectors/track");
  798. puts("v\tverify the partition table");
  799. puts("w\twrite table to disk and exit");
  800. } else if (LABEL_IS_AIX) {
  801. puts("b\tmove beginning of data in a partition"); /* !sun */
  802. puts("c\tchange number of cylinders");
  803. puts("d\tprint the raw data in the partition table");
  804. puts("e\tlist extended partitions"); /* !sun */
  805. puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
  806. puts("h\tchange number of heads");
  807. puts("p\tprint the partition table");
  808. puts("q\tquit without saving changes");
  809. puts("r\treturn to main menu");
  810. puts("s\tchange number of sectors/track");
  811. puts("v\tverify the partition table");
  812. puts("w\twrite table to disk and exit");
  813. } else {
  814. puts("b\tmove beginning of data in a partition"); /* !sun */
  815. puts("c\tchange number of cylinders");
  816. puts("d\tprint the raw data in the partition table");
  817. puts("e\tlist extended partitions"); /* !sun */
  818. puts("f\tfix partition order"); /* !sun, !aix, !sgi */
  819. #if ENABLE_FEATURE_SGI_LABEL
  820. puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
  821. #endif
  822. puts("h\tchange number of heads");
  823. puts("p\tprint the partition table");
  824. puts("q\tquit without saving changes");
  825. puts("r\treturn to main menu");
  826. puts("s\tchange number of sectors/track");
  827. puts("v\tverify the partition table");
  828. puts("w\twrite table to disk and exit");
  829. }
  830. }
  831. #endif /* ADVANCED mode */
  832. #if ENABLE_FEATURE_FDISK_WRITABLE
  833. static const char *const *
  834. get_sys_types(void)
  835. {
  836. return (
  837. LABEL_IS_SUN ? sun_sys_types :
  838. LABEL_IS_SGI ? sgi_sys_types :
  839. i386_sys_types);
  840. }
  841. #else
  842. #define get_sys_types() i386_sys_types
  843. #endif
  844. static const char *
  845. partition_type(unsigned char type)
  846. {
  847. int i;
  848. const char *const *types = get_sys_types();
  849. for (i = 0; types[i]; i++)
  850. if ((unsigned char)types[i][0] == type)
  851. return types[i] + 1;
  852. return "Unknown";
  853. }
  854. static int
  855. is_cleared_partition(const struct partition *p)
  856. {
  857. /* We consider partition "cleared" only if it has only zeros */
  858. const char *cp = (const char *)p;
  859. int cnt = sizeof(*p);
  860. char bits = 0;
  861. while (--cnt >= 0)
  862. bits |= *cp++;
  863. return (bits == 0);
  864. }
  865. static void
  866. clear_partition(struct partition *p)
  867. {
  868. if (p)
  869. memset(p, 0, sizeof(*p));
  870. }
  871. #if ENABLE_FEATURE_FDISK_WRITABLE
  872. static int
  873. get_sysid(int i)
  874. {
  875. return LABEL_IS_SUN ? sunlabel->infos[i].id :
  876. (LABEL_IS_SGI ? sgi_get_sysid(i) :
  877. ptes[i].part_table->sys_ind);
  878. }
  879. static void
  880. list_types(const char *const *sys)
  881. {
  882. enum { COLS = 3 };
  883. unsigned last[COLS];
  884. unsigned done, next, size;
  885. int i;
  886. for (size = 0; sys[size]; size++)
  887. continue;
  888. done = 0;
  889. for (i = COLS-1; i >= 0; i--) {
  890. done += (size + i - done) / (i + 1);
  891. last[COLS-1 - i] = done;
  892. }
  893. i = done = next = 0;
  894. do {
  895. printf("%c%2x %-22.22s", i ? ' ' : '\n',
  896. (unsigned char)sys[next][0],
  897. sys[next] + 1);
  898. next = last[i++] + done;
  899. if (i >= COLS || next >= last[i]) {
  900. i = 0;
  901. next = ++done;
  902. }
  903. } while (done < last[0]);
  904. bb_putchar('\n');
  905. }
  906. #define set_hsc(h, s, c, sector) do \
  907. { \
  908. s = sector % g_sectors + 1; \
  909. sector /= g_sectors; \
  910. h = sector % g_heads; \
  911. sector /= g_heads; \
  912. c = sector & 0xff; \
  913. s |= (sector >> 2) & 0xc0; \
  914. } while (0)
  915. static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
  916. {
  917. if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
  918. start = g_heads * g_sectors * 1024 - 1;
  919. set_hsc(p->head, p->sector, p->cyl, start);
  920. if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
  921. stop = g_heads * g_sectors * 1024 - 1;
  922. set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
  923. }
  924. static void
  925. set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
  926. {
  927. struct partition *p;
  928. sector_t offset;
  929. if (doext) {
  930. p = ptes[i].ext_pointer;
  931. offset = extended_offset;
  932. } else {
  933. p = ptes[i].part_table;
  934. offset = ptes[i].offset_from_dev_start;
  935. }
  936. p->boot_ind = 0;
  937. p->sys_ind = sysid;
  938. set_start_sect(p, start - offset);
  939. set_nr_sects(p, stop - start + 1);
  940. set_hsc_start_end(p, start, stop);
  941. ptes[i].changed = 1;
  942. }
  943. #endif
  944. static int
  945. warn_geometry(void)
  946. {
  947. if (g_heads && g_sectors && g_cylinders)
  948. return 0;
  949. printf("Unknown value(s) for:");
  950. if (!g_heads)
  951. printf(" heads");
  952. if (!g_sectors)
  953. printf(" sectors");
  954. if (!g_cylinders)
  955. printf(" cylinders");
  956. printf(
  957. #if ENABLE_FEATURE_FDISK_WRITABLE
  958. " (settable in the extra functions menu)"
  959. #endif
  960. "\n");
  961. return 1;
  962. }
  963. static void
  964. update_units(void)
  965. {
  966. int cyl_units = g_heads * g_sectors;
  967. if (display_in_cyl_units && cyl_units)
  968. units_per_sector = cyl_units;
  969. else
  970. units_per_sector = 1; /* in sectors */
  971. }
  972. #if ENABLE_FEATURE_FDISK_WRITABLE
  973. static void
  974. warn_cylinders(void)
  975. {
  976. if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
  977. printf("\n"
  978. "The number of cylinders for this disk is set to %u.\n"
  979. "There is nothing wrong with that, but this is larger than 1024,\n"
  980. "and could in certain setups cause problems with:\n"
  981. "1) software that runs at boot time (e.g., old versions of LILO)\n"
  982. "2) booting and partitioning software from other OSs\n"
  983. " (e.g., DOS FDISK, OS/2 FDISK)\n",
  984. g_cylinders);
  985. }
  986. #endif
  987. static void
  988. read_extended(int ext)
  989. {
  990. int i;
  991. struct pte *pex;
  992. struct partition *p, *q;
  993. ext_index = ext;
  994. pex = &ptes[ext];
  995. pex->ext_pointer = pex->part_table;
  996. p = pex->part_table;
  997. if (!get_start_sect(p)) {
  998. printf("Bad offset in primary extended partition\n");
  999. return;
  1000. }
  1001. while (IS_EXTENDED(p->sys_ind)) {
  1002. struct pte *pe = &ptes[g_partitions];
  1003. if (g_partitions >= MAXIMUM_PARTS) {
  1004. /* This is not a Linux restriction, but
  1005. this program uses arrays of size MAXIMUM_PARTS.
  1006. Do not try to 'improve' this test. */
  1007. struct pte *pre = &ptes[g_partitions - 1];
  1008. #if ENABLE_FEATURE_FDISK_WRITABLE
  1009. printf("Warning: deleting partitions after %u\n",
  1010. g_partitions);
  1011. pre->changed = 1;
  1012. #endif
  1013. clear_partition(pre->ext_pointer);
  1014. return;
  1015. }
  1016. read_pte(pe, extended_offset + get_start_sect(p));
  1017. if (!extended_offset)
  1018. extended_offset = get_start_sect(p);
  1019. q = p = pt_offset(pe->sectorbuffer, 0);
  1020. for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
  1021. if (IS_EXTENDED(p->sys_ind)) {
  1022. if (pe->ext_pointer)
  1023. printf("Warning: extra link "
  1024. "pointer in partition table"
  1025. " %u\n", g_partitions + 1);
  1026. else
  1027. pe->ext_pointer = p;
  1028. } else if (p->sys_ind) {
  1029. if (pe->part_table)
  1030. printf("Warning: ignoring extra "
  1031. "data in partition table"
  1032. " %u\n", g_partitions + 1);
  1033. else
  1034. pe->part_table = p;
  1035. }
  1036. }
  1037. /* very strange code here... */
  1038. if (!pe->part_table) {
  1039. if (q != pe->ext_pointer)
  1040. pe->part_table = q;
  1041. else
  1042. pe->part_table = q + 1;
  1043. }
  1044. if (!pe->ext_pointer) {
  1045. if (q != pe->part_table)
  1046. pe->ext_pointer = q;
  1047. else
  1048. pe->ext_pointer = q + 1;
  1049. }
  1050. p = pe->ext_pointer;
  1051. g_partitions++;
  1052. }
  1053. #if ENABLE_FEATURE_FDISK_WRITABLE
  1054. /* remove empty links */
  1055. remove:
  1056. for (i = 4; i < g_partitions; i++) {
  1057. struct pte *pe = &ptes[i];
  1058. if (!get_nr_sects(pe->part_table)
  1059. && (g_partitions > 5 || ptes[4].part_table->sys_ind)
  1060. ) {
  1061. printf("Omitting empty partition (%u)\n", i+1);
  1062. delete_partition(i);
  1063. goto remove; /* numbering changed */
  1064. }
  1065. }
  1066. #endif
  1067. }
  1068. #if ENABLE_FEATURE_FDISK_WRITABLE
  1069. static void
  1070. create_doslabel(void)
  1071. {
  1072. printf(msg_building_new_label, "DOS disklabel");
  1073. current_label_type = LABEL_DOS;
  1074. #if ENABLE_FEATURE_OSF_LABEL
  1075. possibly_osf_label = 0;
  1076. #endif
  1077. g_partitions = 4;
  1078. memset(&MBRbuffer[510 - 4*16], 0, 4*16);
  1079. write_part_table_flag(MBRbuffer);
  1080. extended_offset = 0;
  1081. set_all_unchanged();
  1082. set_changed(0);
  1083. get_boot(CREATE_EMPTY_DOS);
  1084. }
  1085. #endif
  1086. static void
  1087. get_sectorsize(void)
  1088. {
  1089. if (!user_set_sector_size) {
  1090. int arg;
  1091. if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
  1092. sector_size = arg;
  1093. if (sector_size != DEFAULT_SECTOR_SIZE)
  1094. printf("Note: sector size is %u "
  1095. "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
  1096. sector_size);
  1097. }
  1098. }
  1099. static void
  1100. get_kernel_geometry(void)
  1101. {
  1102. struct hd_geometry geometry;
  1103. if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
  1104. kern_heads = geometry.heads;
  1105. kern_sectors = geometry.sectors;
  1106. /* never use geometry.cylinders - it is truncated */
  1107. }
  1108. }
  1109. static void
  1110. get_partition_table_geometry(void)
  1111. {
  1112. const unsigned char *bufp = (const unsigned char *)MBRbuffer;
  1113. struct partition *p;
  1114. int i, h, s, hh, ss;
  1115. int first = 1;
  1116. int bad = 0;
  1117. if (!(valid_part_table_flag((char*)bufp)))
  1118. return;
  1119. hh = ss = 0;
  1120. for (i = 0; i < 4; i++) {
  1121. p = pt_offset(bufp, i);
  1122. if (p->sys_ind != 0) {
  1123. h = p->end_head + 1;
  1124. s = (p->end_sector & 077);
  1125. if (first) {
  1126. hh = h;
  1127. ss = s;
  1128. first = 0;
  1129. } else if (hh != h || ss != s)
  1130. bad = 1;
  1131. }
  1132. }
  1133. if (!first && !bad) {
  1134. pt_heads = hh;
  1135. pt_sectors = ss;
  1136. }
  1137. }
  1138. static void
  1139. get_geometry(void)
  1140. {
  1141. int sec_fac;
  1142. get_sectorsize();
  1143. sec_fac = sector_size / 512;
  1144. #if ENABLE_FEATURE_SUN_LABEL
  1145. guess_device_type();
  1146. #endif
  1147. g_heads = g_cylinders = g_sectors = 0;
  1148. kern_heads = kern_sectors = 0;
  1149. pt_heads = pt_sectors = 0;
  1150. get_kernel_geometry();
  1151. get_partition_table_geometry();
  1152. g_heads = user_heads ? user_heads :
  1153. pt_heads ? pt_heads :
  1154. kern_heads ? kern_heads : 255;
  1155. g_sectors = user_sectors ? user_sectors :
  1156. pt_sectors ? pt_sectors :
  1157. kern_sectors ? kern_sectors : 63;
  1158. total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
  1159. sector_offset = 1;
  1160. if (dos_compatible_flag)
  1161. sector_offset = g_sectors;
  1162. g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
  1163. if (!g_cylinders)
  1164. g_cylinders = user_cylinders;
  1165. }
  1166. /*
  1167. * Opens disk_device and optionally reads MBR.
  1168. * FIXME: document what each 'what' value will do!
  1169. * Returns:
  1170. * -1: no 0xaa55 flag present (possibly entire disk BSD)
  1171. * 0: found or created label
  1172. * 1: I/O error
  1173. */
  1174. #if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
  1175. static int get_boot(enum action what)
  1176. #else
  1177. static int get_boot(void)
  1178. #define get_boot(what) get_boot()
  1179. #endif
  1180. {
  1181. int i, fd;
  1182. g_partitions = 4;
  1183. for (i = 0; i < 4; i++) {
  1184. struct pte *pe = &ptes[i];
  1185. pe->part_table = pt_offset(MBRbuffer, i);
  1186. pe->ext_pointer = NULL;
  1187. pe->offset_from_dev_start = 0;
  1188. pe->sectorbuffer = MBRbuffer;
  1189. #if ENABLE_FEATURE_FDISK_WRITABLE
  1190. pe->changed = (what == CREATE_EMPTY_DOS);
  1191. #endif
  1192. }
  1193. #if ENABLE_FEATURE_FDISK_WRITABLE
  1194. // ALERT! highly idiotic design!
  1195. // We end up here when we call get_boot() recursively
  1196. // via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
  1197. // or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
  1198. // (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
  1199. // So skip opening device _again_...
  1200. if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
  1201. goto created_table;
  1202. fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
  1203. if (fd < 0) {
  1204. fd = open(disk_device, O_RDONLY);
  1205. if (fd < 0) {
  1206. if (what == TRY_ONLY)
  1207. return 1;
  1208. fdisk_fatal(unable_to_open);
  1209. }
  1210. printf("'%s' is opened for read only\n", disk_device);
  1211. }
  1212. xmove_fd(fd, dev_fd);
  1213. if (512 != full_read(dev_fd, MBRbuffer, 512)) {
  1214. if (what == TRY_ONLY) {
  1215. close_dev_fd();
  1216. return 1;
  1217. }
  1218. fdisk_fatal(unable_to_read);
  1219. }
  1220. #else
  1221. fd = open(disk_device, O_RDONLY);
  1222. if (fd < 0)
  1223. return 1;
  1224. if (512 != full_read(fd, MBRbuffer, 512)) {
  1225. close(fd);
  1226. return 1;
  1227. }
  1228. xmove_fd(fd, dev_fd);
  1229. #endif
  1230. get_geometry();
  1231. update_units();
  1232. #if ENABLE_FEATURE_SUN_LABEL
  1233. if (check_sun_label())
  1234. return 0;
  1235. #endif
  1236. #if ENABLE_FEATURE_SGI_LABEL
  1237. if (check_sgi_label())
  1238. return 0;
  1239. #endif
  1240. #if ENABLE_FEATURE_AIX_LABEL
  1241. if (check_aix_label())
  1242. return 0;
  1243. #endif
  1244. #if ENABLE_FEATURE_OSF_LABEL
  1245. if (check_osf_label()) {
  1246. possibly_osf_label = 1;
  1247. if (!valid_part_table_flag(MBRbuffer)) {
  1248. current_label_type = LABEL_OSF;
  1249. return 0;
  1250. }
  1251. printf("This disk has both DOS and BSD magic.\n"
  1252. "Give the 'b' command to go to BSD mode.\n");
  1253. }
  1254. #endif
  1255. #if !ENABLE_FEATURE_FDISK_WRITABLE
  1256. if (!valid_part_table_flag(MBRbuffer))
  1257. return -1;
  1258. #else
  1259. if (!valid_part_table_flag(MBRbuffer)) {
  1260. if (what == OPEN_MAIN) {
  1261. printf("Device contains neither a valid DOS "
  1262. "partition table, nor Sun, SGI or OSF "
  1263. "disklabel\n");
  1264. #ifdef __sparc__
  1265. IF_FEATURE_SUN_LABEL(create_sunlabel();)
  1266. #else
  1267. create_doslabel();
  1268. #endif
  1269. return 0;
  1270. }
  1271. /* TRY_ONLY: */
  1272. return -1;
  1273. }
  1274. created_table:
  1275. #endif /* FEATURE_FDISK_WRITABLE */
  1276. IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
  1277. warn_geometry();
  1278. for (i = 0; i < 4; i++) {
  1279. if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
  1280. if (g_partitions != 4)
  1281. printf("Ignoring extra extended "
  1282. "partition %u\n", i + 1);
  1283. else
  1284. read_extended(i);
  1285. }
  1286. }
  1287. for (i = 3; i < g_partitions; i++) {
  1288. struct pte *pe = &ptes[i];
  1289. if (!valid_part_table_flag(pe->sectorbuffer)) {
  1290. printf("Warning: invalid flag 0x%02x,0x%02x of partition "
  1291. "table %u will be corrected by w(rite)\n",
  1292. pe->sectorbuffer[510],
  1293. pe->sectorbuffer[511],
  1294. i + 1);
  1295. IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
  1296. }
  1297. }
  1298. return 0;
  1299. }
  1300. #if ENABLE_FEATURE_FDISK_WRITABLE
  1301. /*
  1302. * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
  1303. * If the user hits Enter, DFLT is returned.
  1304. * Answers like +10 are interpreted as offsets from BASE.
  1305. *
  1306. * There is no default if DFLT is not between LOW and HIGH.
  1307. */
  1308. static sector_t
  1309. read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
  1310. {
  1311. sector_t value;
  1312. int default_ok = 1;
  1313. const char *fmt = "%s (%u-%u, default %u): ";
  1314. if (dflt < low || dflt > high) {
  1315. fmt = "%s (%u-%u): ";
  1316. default_ok = 0;
  1317. }
  1318. while (1) {
  1319. int use_default = default_ok;
  1320. /* ask question and read answer */
  1321. do {
  1322. printf(fmt, mesg, low, high, dflt);
  1323. read_maybe_empty("");
  1324. } while (*line_ptr != '\n' && !isdigit(*line_ptr)
  1325. && *line_ptr != '-' && *line_ptr != '+');
  1326. if (*line_ptr == '+' || *line_ptr == '-') {
  1327. int minus = (*line_ptr == '-');
  1328. int absolute = 0;
  1329. value = atoi(line_ptr + 1);
  1330. /* (1) if 2nd char is digit, use_default = 0.
  1331. * (2) move line_ptr to first non-digit. */
  1332. while (isdigit(*++line_ptr))
  1333. use_default = 0;
  1334. switch (*line_ptr) {
  1335. case 'c':
  1336. case 'C':
  1337. if (!display_in_cyl_units)
  1338. value *= g_heads * g_sectors;
  1339. break;
  1340. case 'K':
  1341. absolute = 1024;
  1342. break;
  1343. case 'k':
  1344. absolute = 1000;
  1345. break;
  1346. case 'm':
  1347. case 'M':
  1348. absolute = 1000000;
  1349. break;
  1350. case 'g':
  1351. case 'G':
  1352. absolute = 1000000000;
  1353. break;
  1354. default:
  1355. break;
  1356. }
  1357. if (absolute) {
  1358. ullong bytes;
  1359. unsigned long unit;
  1360. bytes = (ullong) value * absolute;
  1361. unit = sector_size * units_per_sector;
  1362. bytes += unit/2; /* round */
  1363. bytes /= unit;
  1364. value = bytes;
  1365. }
  1366. if (minus)
  1367. value = -value;
  1368. value += base;
  1369. } else {
  1370. value = atoi(line_ptr);
  1371. while (isdigit(*line_ptr)) {
  1372. line_ptr++;
  1373. use_default = 0;
  1374. }
  1375. }
  1376. if (use_default) {
  1377. value = dflt;
  1378. printf("Using default value %u\n", value);
  1379. }
  1380. if (value >= low && value <= high)
  1381. break;
  1382. printf("Value is out of range\n");
  1383. }
  1384. return value;
  1385. }
  1386. static unsigned
  1387. get_partition(int warn, unsigned max)
  1388. {
  1389. struct pte *pe;
  1390. unsigned i;
  1391. i = read_int(1, 0, max, 0, "Partition number") - 1;
  1392. pe = &ptes[i];
  1393. if (warn) {
  1394. if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
  1395. || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
  1396. || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
  1397. ) {
  1398. printf("Warning: partition %u has empty type\n", i+1);
  1399. }
  1400. }
  1401. return i;
  1402. }
  1403. static int
  1404. get_existing_partition(int warn, unsigned max)
  1405. {
  1406. int pno = -1;
  1407. unsigned i;
  1408. for (i = 0; i < max; i++) {
  1409. struct pte *pe = &ptes[i];
  1410. struct partition *p = pe->part_table;
  1411. if (p && !is_cleared_partition(p)) {
  1412. if (pno >= 0)
  1413. goto not_unique;
  1414. pno = i;
  1415. }
  1416. }
  1417. if (pno >= 0) {
  1418. printf("Selected partition %u\n", pno+1);
  1419. return pno;
  1420. }
  1421. printf("No partition is defined yet!\n");
  1422. return -1;
  1423. not_unique:
  1424. return get_partition(warn, max);
  1425. }
  1426. static int
  1427. get_nonexisting_partition(int warn, unsigned max)
  1428. {
  1429. int pno = -1;
  1430. unsigned i;
  1431. for (i = 0; i < max; i++) {
  1432. struct pte *pe = &ptes[i];
  1433. struct partition *p = pe->part_table;
  1434. if (p && is_cleared_partition(p)) {
  1435. if (pno >= 0)
  1436. goto not_unique;
  1437. pno = i;
  1438. }
  1439. }
  1440. if (pno >= 0) {
  1441. printf("Selected partition %u\n", pno+1);
  1442. return pno;
  1443. }
  1444. printf("All primary partitions have been defined already!\n");
  1445. return -1;
  1446. not_unique:
  1447. return get_partition(warn, max);
  1448. }
  1449. static void
  1450. change_units(void)
  1451. {
  1452. display_in_cyl_units = !display_in_cyl_units;
  1453. update_units();
  1454. printf("Changing display/entry units to %s\n",
  1455. str_units(PLURAL));
  1456. }
  1457. static void
  1458. toggle_active(int i)
  1459. {
  1460. struct pte *pe = &ptes[i];
  1461. struct partition *p = pe->part_table;
  1462. if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
  1463. printf("WARNING: Partition %u is an extended partition\n", i + 1);
  1464. p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
  1465. pe->changed = 1;
  1466. }
  1467. static void
  1468. toggle_dos_compatibility_flag(void)
  1469. {
  1470. dos_compatible_flag = 1 - dos_compatible_flag;
  1471. if (dos_compatible_flag) {
  1472. sector_offset = g_sectors;
  1473. printf("DOS Compatibility flag is set\n");
  1474. } else {
  1475. sector_offset = 1;
  1476. printf("DOS Compatibility flag is not set\n");
  1477. }
  1478. }
  1479. static void
  1480. delete_partition(int i)
  1481. {
  1482. struct pte *pe = &ptes[i];
  1483. struct partition *p = pe->part_table;
  1484. struct partition *q = pe->ext_pointer;
  1485. /* Note that for the fifth partition (i == 4) we don't actually
  1486. * decrement partitions.
  1487. */
  1488. if (warn_geometry())
  1489. return; /* C/H/S not set */
  1490. pe->changed = 1;
  1491. if (LABEL_IS_SUN) {
  1492. sun_delete_partition(i);
  1493. return;
  1494. }
  1495. if (LABEL_IS_SGI) {
  1496. sgi_delete_partition(i);
  1497. return;
  1498. }
  1499. if (i < 4) {
  1500. if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
  1501. g_partitions = 4;
  1502. ptes[ext_index].ext_pointer = NULL;
  1503. extended_offset = 0;
  1504. }
  1505. clear_partition(p);
  1506. return;
  1507. }
  1508. if (!q->sys_ind && i > 4) {
  1509. /* the last one in the chain - just delete */
  1510. --g_partitions;
  1511. --i;
  1512. clear_partition(ptes[i].ext_pointer);
  1513. ptes[i].changed = 1;
  1514. } else {
  1515. /* not the last one - further ones will be moved down */
  1516. if (i > 4) {
  1517. /* delete this link in the chain */
  1518. p = ptes[i-1].ext_pointer;
  1519. *p = *q;
  1520. set_start_sect(p, get_start_sect(q));
  1521. set_nr_sects(p, get_nr_sects(q));
  1522. ptes[i-1].changed = 1;
  1523. } else if (g_partitions > 5) { /* 5 will be moved to 4 */
  1524. /* the first logical in a longer chain */
  1525. pe = &ptes[5];
  1526. if (pe->part_table) /* prevent SEGFAULT */
  1527. set_start_sect(pe->part_table,
  1528. get_partition_start_from_dev_start(pe) -
  1529. extended_offset);
  1530. pe->offset_from_dev_start = extended_offset;
  1531. pe->changed = 1;
  1532. }
  1533. if (g_partitions > 5) {
  1534. g_partitions--;
  1535. while (i < g_partitions) {
  1536. ptes[i] = ptes[i+1];
  1537. i++;
  1538. }
  1539. } else {
  1540. /* the only logical: clear only */
  1541. clear_partition(ptes[i].part_table);
  1542. }
  1543. }
  1544. }
  1545. static void
  1546. change_sysid(void)
  1547. {
  1548. int i, sys, origsys;
  1549. struct partition *p;
  1550. /* If sgi_label then don't use get_existing_partition,
  1551. let the user select a partition, since get_existing_partition()
  1552. only works for Linux like partition tables. */
  1553. if (!LABEL_IS_SGI) {
  1554. i = get_existing_partition(0, g_partitions);
  1555. } else {
  1556. i = get_partition(0, g_partitions);
  1557. }
  1558. if (i == -1)
  1559. return;
  1560. p = ptes[i].part_table;
  1561. origsys = sys = get_sysid(i);
  1562. /* if changing types T to 0 is allowed, then
  1563. the reverse change must be allowed, too */
  1564. if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
  1565. printf("Partition %u does not exist yet!\n", i + 1);
  1566. return;
  1567. }
  1568. while (1) {
  1569. sys = read_hex(get_sys_types());
  1570. if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
  1571. printf("Type 0 means free space to many systems\n"
  1572. "(but not to Linux). Having partitions of\n"
  1573. "type 0 is probably unwise.\n");
  1574. /* break; */
  1575. }
  1576. if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
  1577. if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
  1578. printf("You cannot change a partition into"
  1579. " an extended one or vice versa\n");
  1580. break;
  1581. }
  1582. }
  1583. if (sys < 256) {
  1584. #if ENABLE_FEATURE_SUN_LABEL
  1585. if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
  1586. printf("Consider leaving partition 3 "
  1587. "as Whole disk (5),\n"
  1588. "as SunOS/Solaris expects it and "
  1589. "even Linux likes it\n\n");
  1590. #endif
  1591. #if ENABLE_FEATURE_SGI_LABEL
  1592. if (LABEL_IS_SGI &&
  1593. (
  1594. (i == 10 && sys != SGI_ENTIRE_DISK) ||
  1595. (i == 8 && sys != 0)
  1596. )
  1597. ) {
  1598. printf("Consider leaving partition 9 "
  1599. "as volume header (0),\nand "
  1600. "partition 11 as entire volume (6)"
  1601. "as IRIX expects it\n\n");
  1602. }
  1603. #endif
  1604. if (sys == origsys)
  1605. break;
  1606. if (LABEL_IS_SUN) {
  1607. sun_change_sysid(i, sys);
  1608. } else if (LABEL_IS_SGI) {
  1609. sgi_change_sysid(i, sys);
  1610. } else
  1611. p->sys_ind = sys;
  1612. printf("Changed system type of partition %u "
  1613. "to %x (%s)\n", i + 1, sys,
  1614. partition_type(sys));
  1615. ptes[i].changed = 1;
  1616. //if (is_dos_partition(origsys) || is_dos_partition(sys))
  1617. // dos_changed = 1;
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. #endif /* FEATURE_FDISK_WRITABLE */
  1623. /* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
  1624. * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
  1625. * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
  1626. * Lubkin Oct. 1991). */
  1627. static void
  1628. linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
  1629. {
  1630. int spc = g_heads * g_sectors;
  1631. *c = ls / spc;
  1632. ls = ls % spc;
  1633. *h = ls / g_sectors;
  1634. *s = ls % g_sectors + 1; /* sectors count from 1 */
  1635. }
  1636. static void
  1637. check_consistency(const struct partition *p, int partition)
  1638. {
  1639. unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
  1640. unsigned pec, peh, pes; /* physical ending c, h, s */
  1641. unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
  1642. unsigned lec, leh, les; /* logical ending c, h, s */
  1643. if (!g_heads || !g_sectors || (partition >= 4))
  1644. return; /* do not check extended partitions */
  1645. /* physical beginning c, h, s */
  1646. pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
  1647. pbh = p->head;
  1648. pbs = p->sector & 0x3f;
  1649. /* physical ending c, h, s */
  1650. pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
  1651. peh = p->end_head;
  1652. pes = p->end_sector & 0x3f;
  1653. /* compute logical beginning (c, h, s) */
  1654. linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
  1655. /* compute logical ending (c, h, s) */
  1656. linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
  1657. /* Same physical / logical beginning? */
  1658. if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
  1659. printf("Partition %u has different physical/logical "
  1660. "beginnings (non-Linux?):\n", partition + 1);
  1661. printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
  1662. printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
  1663. }
  1664. /* Same physical / logical ending? */
  1665. if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
  1666. printf("Partition %u has different physical/logical "
  1667. "endings:\n", partition + 1);
  1668. printf(" phys=(%u, %u, %u) ", pec, peh, pes);
  1669. printf("logical=(%u, %u, %u)\n", lec, leh, les);
  1670. }
  1671. /* Ending on cylinder boundary? */
  1672. if (peh != (g_heads - 1) || pes != g_sectors) {
  1673. printf("Partition %u does not end on cylinder boundary\n",
  1674. partition + 1);
  1675. }
  1676. }
  1677. static void
  1678. list_disk_geometry(void)
  1679. {
  1680. ullong bytes = ((ullong)total_number_of_sectors << 9);
  1681. long megabytes = bytes / 1000000;
  1682. if (megabytes < 10000)
  1683. printf("\nDisk %s: %lu MB, %llu bytes\n",
  1684. disk_device, megabytes, bytes);
  1685. else
  1686. printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
  1687. disk_device, megabytes/1000, (megabytes/100)%10, bytes);
  1688. printf("%u heads, %u sectors/track, %u cylinders",
  1689. g_heads, g_sectors, g_cylinders);
  1690. if (units_per_sector == 1)
  1691. printf(", total %"SECT_FMT"u sectors",
  1692. total_number_of_sectors / (sector_size/512));
  1693. printf("\nUnits = %s of %u * %u = %u bytes\n\n",
  1694. str_units(PLURAL),
  1695. units_per_sector, sector_size, units_per_sector * sector_size);
  1696. }
  1697. /*
  1698. * Check whether partition entries are ordered by their starting positions.
  1699. * Return 0 if OK. Return i if partition i should have been earlier.
  1700. * Two separate checks: primary and logical partitions.
  1701. */
  1702. static int
  1703. wrong_p_order(int *prev)
  1704. {
  1705. const struct pte *pe;
  1706. const struct partition *p;
  1707. sector_t last_p_start_pos = 0, p_start_pos;
  1708. unsigned i, last_i = 0;
  1709. for (i = 0; i < g_partitions; i++) {
  1710. if (i == 4) {
  1711. last_i = 4;
  1712. last_p_start_pos = 0;
  1713. }
  1714. pe = &ptes[i];
  1715. p = pe->part_table;
  1716. if (p->sys_ind) {
  1717. p_start_pos = get_partition_start_from_dev_start(pe);
  1718. if (last_p_start_pos > p_start_pos) {
  1719. if (prev)
  1720. *prev = last_i;
  1721. return i;
  1722. }
  1723. last_p_start_pos = p_start_pos;
  1724. last_i = i;
  1725. }
  1726. }
  1727. return 0;
  1728. }
  1729. #if ENABLE_FEATURE_FDISK_ADVANCED
  1730. /*
  1731. * Fix the chain of logicals.
  1732. * extended_offset is unchanged, the set of sectors used is unchanged
  1733. * The chain is sorted so that sectors increase, and so that
  1734. * starting sectors increase.
  1735. *
  1736. * After this it may still be that cfdisk doesnt like the table.
  1737. * (This is because cfdisk considers expanded parts, from link to
  1738. * end of partition, and these may still overlap.)
  1739. * Now
  1740. * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
  1741. * may help.
  1742. */
  1743. static void
  1744. fix_chain_of_logicals(void)
  1745. {
  1746. int j, oj, ojj, sj, sjj;
  1747. struct partition *pj,*pjj,tmp;
  1748. /* Stage 1: sort sectors but leave sector of part 4 */
  1749. /* (Its sector is the global extended_offset.) */
  1750. stage1:
  1751. for (j = 5; j < g_partitions - 1; j++) {
  1752. oj = ptes[j].offset_from_dev_start;
  1753. ojj = ptes[j+1].offset_from_dev_start;
  1754. if (oj > ojj) {
  1755. ptes[j].offset_from_dev_start = ojj;
  1756. ptes[j+1].offset_from_dev_start = oj;
  1757. pj = ptes[j].part_table;
  1758. set_start_sect(pj, get_start_sect(pj)+oj-ojj);
  1759. pjj = ptes[j+1].part_table;
  1760. set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
  1761. set_start_sect(ptes[j-1].ext_pointer,
  1762. ojj-extended_offset);
  1763. set_start_sect(ptes[j].ext_pointer,
  1764. oj-extended_offset);
  1765. goto stage1;
  1766. }
  1767. }
  1768. /* Stage 2: sort starting sectors */
  1769. stage2:
  1770. for (j = 4; j < g_partitions - 1; j++) {
  1771. pj = ptes[j].part_table;
  1772. pjj = ptes[j+1].part_table;
  1773. sj = get_start_sect(pj);
  1774. sjj = get_start_sect(pjj);
  1775. oj = ptes[j].offset_from_dev_start;
  1776. ojj = ptes[j+1].offset_from_dev_start;
  1777. if (oj+sj > ojj+sjj) {
  1778. tmp = *pj;
  1779. *pj = *pjj;
  1780. *pjj = tmp;
  1781. set_start_sect(pj, ojj+sjj-oj);
  1782. set_start_sect(pjj, oj+sj-ojj);
  1783. goto stage2;
  1784. }
  1785. }
  1786. /* Probably something was changed */
  1787. for (j = 4; j < g_partitions; j++)
  1788. ptes[j].changed = 1;
  1789. }
  1790. static void
  1791. fix_partition_table_order(void)
  1792. {
  1793. struct pte *pei, *pek;
  1794. int i,k;
  1795. if (!wrong_p_order(NULL)) {
  1796. printf("Ordering is already correct\n\n");
  1797. return;
  1798. }
  1799. while ((i = wrong_p_order(&k)) != 0 && i < 4) {
  1800. /* partition i should have come earlier, move it */
  1801. /* We have to move data in the MBR */
  1802. struct partition *pi, *pk, *pe, pbuf;
  1803. pei = &ptes[i];
  1804. pek = &ptes[k];
  1805. pe = pei->ext_pointer;
  1806. pei->ext_pointer = pek->ext_pointer;
  1807. pek->ext_pointer = pe;
  1808. pi = pei->part_table;
  1809. pk = pek->part_table;
  1810. memmove(&pbuf, pi, sizeof(struct partition));
  1811. memmove(pi, pk, sizeof(struct partition));
  1812. memmove(pk, &pbuf, sizeof(struct partition));
  1813. pei->changed = pek->changed = 1;
  1814. }
  1815. if (i)
  1816. fix_chain_of_logicals();
  1817. printf("Done.\n");
  1818. }
  1819. #endif
  1820. static void
  1821. list_table(int xtra)
  1822. {
  1823. const struct partition *p;
  1824. int i, w;
  1825. if (LABEL_IS_SUN) {
  1826. sun_list_table(xtra);
  1827. return;
  1828. }
  1829. if (LABEL_IS_SUN) {
  1830. sgi_list_table(xtra);
  1831. return;
  1832. }
  1833. list_disk_geometry();
  1834. if (LABEL_IS_OSF) {
  1835. xbsd_print_disklabel(xtra);
  1836. return;
  1837. }
  1838. /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
  1839. but if the device name ends in a digit, say /dev/foo1,
  1840. then the partition is called /dev/foo1p3. */
  1841. w = strlen(disk_device);
  1842. if (w && isdigit(disk_device[w-1]))
  1843. w++;
  1844. if (w < 5)
  1845. w = 5;
  1846. // 1 12345678901 12345678901 12345678901 12
  1847. printf("%*s Boot Start End Blocks Id System\n",
  1848. w+1, "Device");
  1849. for (i = 0; i < g_partitions; i++) {
  1850. const struct pte *pe = &ptes[i];
  1851. sector_t psects;
  1852. sector_t pblocks;
  1853. unsigned podd;
  1854. p = pe->part_table;
  1855. if (!p || is_cleared_partition(p))
  1856. continue;
  1857. psects = get_nr_sects(p);
  1858. pblocks = psects;
  1859. podd = 0;
  1860. if (sector_size < 1024) {
  1861. pblocks /= (1024 / sector_size);
  1862. podd = psects % (1024 / sector_size);
  1863. }
  1864. if (sector_size > 1024)
  1865. pblocks *= (sector_size / 1024);
  1866. printf("%s %c %11"SECT_FMT"u %11"SECT_FMT"u %11"SECT_FMT"u%c %2x %s\n",
  1867. partname(disk_device, i+1, w+2),
  1868. !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
  1869. ? '*' : '?',
  1870. cround(get_partition_start_from_dev_start(pe)), /* start */
  1871. cround(get_partition_start_from_dev_start(pe) + psects /* end */
  1872. - (psects ? 1 : 0)),
  1873. pblocks, podd ? '+' : ' ', /* odd flag on end */
  1874. p->sys_ind, /* type id */
  1875. partition_type(p->sys_ind)); /* type name */
  1876. check_consistency(p, i);
  1877. }
  1878. /* Is partition table in disk order? It need not be, but... */
  1879. /* partition table entries are not checked for correct order
  1880. * if this is a sgi, sun or aix labeled disk... */
  1881. if (LABEL_IS_DOS && wrong_p_order(NULL)) {
  1882. /* FIXME */
  1883. printf("\nPartition table entries are not in disk order\n");
  1884. }
  1885. }
  1886. #if ENABLE_FEATURE_FDISK_ADVANCED
  1887. static void
  1888. x_list_table(int extend)
  1889. {
  1890. const struct pte *pe;
  1891. const struct partition *p;
  1892. int i;
  1893. printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
  1894. disk_device, g_heads, g_sectors, g_cylinders);
  1895. printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
  1896. for (i = 0; i < g_partitions; i++) {
  1897. pe = &ptes[i];
  1898. p = (extend ? pe->ext_pointer : pe->part_table);
  1899. if (p != NULL) {
  1900. printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n",
  1901. i + 1, p->boot_ind, p->head,
  1902. sector(p->sector),
  1903. cylinder(p->sector, p->cyl), p->end_head,
  1904. sector(p->end_sector),
  1905. cylinder(p->end_sector, p->end_cyl),
  1906. get_start_sect(p), get_nr_sects(p),
  1907. p->sys_ind);
  1908. if (p->sys_ind)
  1909. check_consistency(p, i);
  1910. }
  1911. }
  1912. }
  1913. #endif
  1914. #if ENABLE_FEATURE_FDISK_WRITABLE
  1915. static void
  1916. fill_bounds(sector_t *first, sector_t *last)
  1917. {
  1918. unsigned i;
  1919. const struct pte *pe = &ptes[0];
  1920. const struct partition *p;
  1921. for (i = 0; i < g_partitions; pe++,i++) {
  1922. p = pe->part_table;
  1923. if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
  1924. first[i] = 0xffffffff;
  1925. last[i] = 0;
  1926. } else {
  1927. first[i] = get_partition_start_from_dev_start(pe);
  1928. last[i] = first[i] + get_nr_sects(p) - 1;
  1929. }
  1930. }
  1931. }
  1932. static void
  1933. check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
  1934. {
  1935. sector_t total, real_s, real_c;
  1936. real_s = sector(s) - 1;
  1937. real_c = cylinder(s, c);
  1938. total = (real_c * g_sectors + real_s) * g_heads + h;
  1939. if (!total)
  1940. printf("Partition %u contains sector 0\n", n);
  1941. if (h >= g_heads)
  1942. printf("Partition %u: head %u greater than maximum %u\n",
  1943. n, h + 1, g_heads);
  1944. if (real_s >= g_sectors)
  1945. printf("Partition %u: sector %u greater than "
  1946. "maximum %u\n", n, s, g_sectors);
  1947. if (real_c >= g_cylinders)
  1948. printf("Partition %u: cylinder %"SECT_FMT"u greater than "
  1949. "maximum %u\n", n, real_c + 1, g_cylinders);
  1950. if (g_cylinders <= 1024 && start != total)
  1951. printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
  1952. "total %"SECT_FMT"u\n", n, start, total);
  1953. }
  1954. static void
  1955. verify(void)
  1956. {
  1957. int i, j;
  1958. sector_t total = 1;
  1959. sector_t first[g_partitions], last[g_partitions];
  1960. struct partition *p;
  1961. if (warn_geometry())
  1962. return;
  1963. if (LABEL_IS_SUN) {
  1964. verify_sun();
  1965. return;
  1966. }
  1967. if (LABEL_IS_SGI) {
  1968. verify_sgi(1);
  1969. return;
  1970. }
  1971. fill_bounds(first, last);
  1972. for (i = 0; i < g_partitions; i++) {
  1973. struct pte *pe = &ptes[i];
  1974. p = pe->part_table;
  1975. if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
  1976. check_consistency(p, i);
  1977. if (get_partition_start_from_dev_start(pe) < first[i])
  1978. printf("Warning: bad start-of-data in "
  1979. "partition %u\n", i + 1);
  1980. check(i + 1, p->end_head, p->end_sector, p->end_cyl,
  1981. last[i]);
  1982. total += last[i] + 1 - first[i];
  1983. for (j = 0; j < i; j++) {
  1984. if ((first[i] >= first[j] && first[i] <= last[j])
  1985. || ((last[i] <= last[j] && last[i] >= first[j]))) {
  1986. printf("Warning: partition %u overlaps "
  1987. "partition %u\n", j + 1, i + 1);
  1988. total += first[i] >= first[j] ?
  1989. first[i] : first[j];
  1990. total -= last[i] <= last[j] ?
  1991. last[i] : last[j];
  1992. }
  1993. }
  1994. }
  1995. }
  1996. if (extended_offset) {
  1997. struct pte *pex = &ptes[ext_index];
  1998. sector_t e_last = get_start_sect(pex->part_table) +
  1999. get_nr_sects(pex->part_table) - 1;
  2000. for (i = 4; i < g_partitions; i++) {
  2001. total++;
  2002. p = ptes[i].part_table;
  2003. if (!p->sys_ind) {
  2004. if (i != 4 || i + 1 < g_partitions)
  2005. printf("Warning: partition %u "
  2006. "is empty\n", i + 1);
  2007. } else if (first[i] < extended_offset || last[i] > e_last) {
  2008. printf("Logical partition %u not entirely in "
  2009. "partition %u\n", i + 1, ext_index + 1);
  2010. }
  2011. }
  2012. }
  2013. if (total > g_heads * g_sectors * g_cylinders)
  2014. printf("Total allocated sectors %u greater than the maximum "
  2015. "%u\n", total, g_heads * g_sectors * g_cylinders);
  2016. else {
  2017. total = g_heads * g_sectors * g_cylinders - total;
  2018. if (total != 0)
  2019. printf("%"SECT_FMT"u unallocated sectors\n", total);
  2020. }
  2021. }
  2022. static void
  2023. add_partition(int n, int sys)
  2024. {
  2025. char mesg[256]; /* 48 does not suffice in Japanese */
  2026. int i, num_read = 0;
  2027. struct partition *p = ptes[n].part_table;
  2028. struct partition *q = ptes[ext_index].part_table;
  2029. sector_t limit, temp;
  2030. sector_t start, stop = 0;
  2031. sector_t first[g_partitions], last[g_partitions];
  2032. if (p && p->sys_ind) {
  2033. printf(msg_part_already_defined, n + 1);
  2034. return;
  2035. }
  2036. fill_bounds(first, last);
  2037. if (n < 4) {
  2038. start = sector_offset;
  2039. if (display_in_cyl_units || !total_number_of_sectors)
  2040. limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
  2041. else
  2042. limit = total_number_of_sectors - 1;
  2043. if (extended_offset) {
  2044. first[ext_index] = extended_offset;
  2045. last[ext_index] = get_start_sect(q) +
  2046. get_nr_sects(q) - 1;
  2047. }
  2048. } else {
  2049. start = extended_offset + sector_offset;
  2050. limit = get_start_sect(q) + get_nr_sects(q) - 1;
  2051. }
  2052. if (display_in_cyl_units)
  2053. for (i = 0; i < g_partitions; i++)
  2054. first[i] = (cround(first[i]) - 1) * units_per_sector;
  2055. snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
  2056. do {
  2057. temp = start;
  2058. for (i = 0; i < g_partitions; i++) {
  2059. int lastplusoff;
  2060. if (start == ptes[i].offset_from_dev_start)
  2061. start += sector_offset;
  2062. lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
  2063. if (start >= first[i] && start <= lastplusoff)
  2064. start = lastplusoff + 1;
  2065. }
  2066. if (start > limit)
  2067. break;
  2068. if (start >= temp+units_per_sector && num_read) {
  2069. printf("Sector %"SECT_FMT"u is already allocated\n", temp);
  2070. temp = start;
  2071. num_read = 0;
  2072. }
  2073. if (!num_read && start == temp) {
  2074. sector_t saved_start;
  2075. saved_start = start;
  2076. start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
  2077. if (display_in_cyl_units) {
  2078. start = (start - 1) * units_per_sector;
  2079. if (start < saved_start)
  2080. start = saved_start;
  2081. }
  2082. num_read = 1;
  2083. }
  2084. } while (start != temp || !num_read);
  2085. if (n > 4) { /* NOT for fifth partition */
  2086. struct pte *pe = &ptes[n];
  2087. pe->offset_from_dev_start = start - sector_offset;
  2088. if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
  2089. pe->offset_from_dev_start++;
  2090. if (sector_offset == 1)
  2091. start++;
  2092. }
  2093. }
  2094. for (i = 0; i < g_partitions; i++) {
  2095. struct pte *pe = &ptes[i];
  2096. if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
  2097. limit = pe->offset_from_dev_start - 1;
  2098. if (start < first[i] && limit >= first[i])
  2099. limit = first[i] - 1;
  2100. }
  2101. if (start > limit) {
  2102. printf("No free sectors available\n");
  2103. if (n > 4)
  2104. g_partitions--;
  2105. return;
  2106. }
  2107. if (cround(start) == cround(limit)) {
  2108. stop = limit;
  2109. } else {
  2110. snprintf(mesg, sizeof(mesg),
  2111. "Last %s or +size or +sizeM or +sizeK",
  2112. str_units(SINGULAR));
  2113. stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
  2114. if (display_in_cyl_units) {
  2115. stop = stop * units_per_sector - 1;
  2116. if (stop >limit)
  2117. stop = limit;
  2118. }
  2119. }
  2120. set_partition(n, 0, start, stop, sys);
  2121. if (n > 4)
  2122. set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
  2123. if (IS_EXTENDED(sys)) {
  2124. struct pte *pe4 = &ptes[4];
  2125. struct pte *pen = &ptes[n];
  2126. ext_index = n;
  2127. pen->ext_pointer = p;
  2128. pe4->offset_from_dev_start = extended_offset = start;
  2129. pe4->sectorbuffer = xzalloc(sector_size);
  2130. pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
  2131. pe4->ext_pointer = pe4->part_table + 1;
  2132. pe4->changed = 1;
  2133. g_partitions = 5;
  2134. }
  2135. }
  2136. static void
  2137. add_logical(void)
  2138. {
  2139. if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
  2140. struct pte *pe = &ptes[g_partitions];
  2141. pe->sectorbuffer = xzalloc(sector_size);
  2142. pe->part_table = pt_offset(pe->sectorbuffer, 0);
  2143. pe->ext_pointer = pe->part_table + 1;
  2144. pe->offset_from_dev_start = 0;
  2145. pe->changed = 1;
  2146. g_partitions++;
  2147. }
  2148. add_partition(g_partitions - 1, LINUX_NATIVE);
  2149. }
  2150. static void
  2151. new_partition(void)
  2152. {
  2153. int i, free_primary = 0;
  2154. if (warn_geometry())
  2155. return;
  2156. if (LABEL_IS_SUN) {
  2157. add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
  2158. return;
  2159. }
  2160. if (LABEL_IS_SGI) {
  2161. sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
  2162. return;
  2163. }
  2164. if (LABEL_IS_AIX) {
  2165. printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
  2166. "If you want to add DOS-type partitions, create a new empty DOS partition\n"
  2167. "table first (use 'o'). This will destroy the present disk contents.\n");
  2168. return;
  2169. }
  2170. for (i = 0; i < 4; i++)
  2171. free_primary += !ptes[i].part_table->sys_ind;
  2172. if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
  2173. printf("The maximum number of partitions has been created\n");
  2174. return;
  2175. }
  2176. if (!free_primary) {
  2177. if (extended_offset)
  2178. add_logical();
  2179. else
  2180. printf("You must delete some partition and add "
  2181. "an extended partition first\n");
  2182. } else {
  2183. char c, line[80];
  2184. snprintf(line, sizeof(line),
  2185. "Command action\n"
  2186. " %s\n"
  2187. " p primary partition (1-4)\n",
  2188. (extended_offset ?
  2189. "l logical (5 or over)" : "e extended"));
  2190. while (1) {
  2191. c = read_nonempty(line);
  2192. if ((c | 0x20) == 'p') {
  2193. i = get_nonexisting_partition(0, 4);
  2194. if (i >= 0)
  2195. add_partition(i, LINUX_NATIVE);
  2196. return;
  2197. }
  2198. if (c == 'l' && extended_offset) {
  2199. add_logical();
  2200. return;
  2201. }
  2202. if (c == 'e' && !extended_offset) {
  2203. i = get_nonexisting_partition(0, 4);
  2204. if (i >= 0)
  2205. add_partition(i, EXTENDED);
  2206. return;
  2207. }
  2208. printf("Invalid partition number "
  2209. "for type '%c'\n", c);
  2210. }
  2211. }
  2212. }
  2213. static void
  2214. write_table(void)
  2215. {
  2216. int i;
  2217. if (LABEL_IS_DOS) {
  2218. for (i = 0; i < 3; i++)
  2219. if (ptes[i].changed)
  2220. ptes[3].changed = 1;
  2221. for (i = 3; i < g_partitions; i++) {
  2222. struct pte *pe = &ptes[i];
  2223. if (pe->changed) {
  2224. write_part_table_flag(pe->sectorbuffer);
  2225. write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
  2226. }
  2227. }
  2228. }
  2229. else if (LABEL_IS_SGI) {
  2230. /* no test on change? the printf below might be mistaken */
  2231. sgi_write_table();
  2232. }
  2233. else if (LABEL_IS_SUN) {
  2234. int needw = 0;
  2235. for (i = 0; i < 8; i++)
  2236. if (ptes[i].changed)
  2237. needw = 1;
  2238. if (needw)
  2239. sun_write_table();
  2240. }
  2241. printf("The partition table has been altered!\n\n");
  2242. reread_partition_table(1);
  2243. }
  2244. static void
  2245. reread_partition_table(int leave)
  2246. {
  2247. int i;
  2248. printf("Calling ioctl() to re-read partition table\n");
  2249. sync();
  2250. /* sleep(2); Huh? */
  2251. i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
  2252. "WARNING: rereading partition table "
  2253. "failed, kernel still uses old table");
  2254. #if 0
  2255. if (dos_changed)
  2256. printf(
  2257. "\nWARNING: If you have created or modified any DOS 6.x\n"
  2258. "partitions, please see the fdisk manual page for additional\n"
  2259. "information\n");
  2260. #endif
  2261. if (leave) {
  2262. if (ENABLE_FEATURE_CLEAN_UP)
  2263. close_dev_fd();
  2264. exit(i != 0);
  2265. }
  2266. }
  2267. #endif /* FEATURE_FDISK_WRITABLE */
  2268. #if ENABLE_FEATURE_FDISK_ADVANCED
  2269. #define MAX_PER_LINE 16
  2270. static void
  2271. print_buffer(char *pbuffer)
  2272. {
  2273. int i,l;
  2274. for (i = 0, l = 0; i < sector_size; i++, l++) {
  2275. if (l == 0)
  2276. printf("0x%03X:", i);
  2277. printf(" %02X", (unsigned char) pbuffer[i]);
  2278. if (l == MAX_PER_LINE - 1) {
  2279. bb_putchar('\n');
  2280. l = -1;
  2281. }
  2282. }
  2283. if (l > 0)
  2284. bb_putchar('\n');
  2285. bb_putchar('\n');
  2286. }
  2287. static void
  2288. print_raw(void)
  2289. {
  2290. int i;
  2291. printf("Device: %s\n", disk_device);
  2292. if (LABEL_IS_SGI || LABEL_IS_SUN)
  2293. print_buffer(MBRbuffer);
  2294. else {
  2295. for (i = 3; i < g_partitions; i++)
  2296. print_buffer(ptes[i].sectorbuffer);
  2297. }
  2298. }
  2299. static void
  2300. move_begin(unsigned i)
  2301. {
  2302. struct pte *pe = &ptes[i];
  2303. struct partition *p = pe->part_table;
  2304. sector_t new, first, nr_sects;
  2305. if (warn_geometry())
  2306. return;
  2307. nr_sects = get_nr_sects(p);
  2308. if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
  2309. printf("Partition %u has no data area\n", i + 1);
  2310. return;
  2311. }
  2312. first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
  2313. new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
  2314. if (new != first) {
  2315. sector_t new_relative = new - pe->offset_from_dev_start;
  2316. nr_sects += (get_start_sect(p) - new_relative);
  2317. set_start_sect(p, new_relative);
  2318. set_nr_sects(p, nr_sects);
  2319. read_nonempty("Recalculate C/H/S values? (Y/N): ");
  2320. if ((line_ptr[0] | 0x20) == 'y')
  2321. set_hsc_start_end(p, new, new + nr_sects - 1);
  2322. pe->changed = 1;
  2323. }
  2324. }
  2325. static void
  2326. xselect(void)
  2327. {
  2328. char c;
  2329. while (1) {
  2330. bb_putchar('\n');
  2331. c = 0x20 | read_nonempty("Expert command (m for help): ");
  2332. switch (c) {
  2333. case 'a':
  2334. if (LABEL_IS_SUN)
  2335. sun_set_alt_cyl();
  2336. break;
  2337. case 'b':
  2338. if (LABEL_IS_DOS)
  2339. move_begin(get_partition(0, g_partitions));
  2340. break;
  2341. case 'c':
  2342. user_cylinders = g_cylinders =
  2343. read_int(1, g_cylinders, 1048576, 0,
  2344. "Number of cylinders");
  2345. if (LABEL_IS_SUN)
  2346. sun_set_ncyl(g_cylinders);
  2347. if (LABEL_IS_DOS)
  2348. warn_cylinders();
  2349. break;
  2350. case 'd':
  2351. print_raw();
  2352. break;
  2353. case 'e':
  2354. if (LABEL_IS_SGI)
  2355. sgi_set_xcyl();
  2356. else if (LABEL_IS_SUN)
  2357. sun_set_xcyl();
  2358. else if (LABEL_IS_DOS)
  2359. x_list_table(1);
  2360. break;
  2361. case 'f':
  2362. if (LABEL_IS_DOS)
  2363. fix_partition_table_order();
  2364. break;
  2365. case 'g':
  2366. #if ENABLE_FEATURE_SGI_LABEL
  2367. create_sgilabel();
  2368. #endif
  2369. break;
  2370. case 'h':
  2371. user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
  2372. update_units();
  2373. break;
  2374. case 'i':
  2375. if (LABEL_IS_SUN)
  2376. sun_set_ilfact();
  2377. break;
  2378. case 'o':
  2379. if (LABEL_IS_SUN)
  2380. sun_set_rspeed();
  2381. break;
  2382. case 'p':
  2383. if (LABEL_IS_SUN)
  2384. list_table(1);
  2385. else
  2386. x_list_table(0);
  2387. break;
  2388. case 'q':
  2389. if (ENABLE_FEATURE_CLEAN_UP)
  2390. close_dev_fd();
  2391. bb_putchar('\n');
  2392. exit(EXIT_SUCCESS);
  2393. case 'r':
  2394. return;
  2395. case 's':
  2396. user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
  2397. if (dos_compatible_flag) {
  2398. sector_offset = g_sectors;
  2399. printf("Warning: setting sector offset for DOS "
  2400. "compatiblity\n");
  2401. }
  2402. update_units();
  2403. break;
  2404. case 'v':
  2405. verify();
  2406. break;
  2407. case 'w':
  2408. write_table(); /* does not return */
  2409. break;
  2410. case 'y':
  2411. if (LABEL_IS_SUN)
  2412. sun_set_pcylcount();
  2413. break;
  2414. default:
  2415. xmenu();
  2416. }
  2417. }
  2418. }
  2419. #endif /* ADVANCED mode */
  2420. static int
  2421. is_ide_cdrom_or_tape(const char *device)
  2422. {
  2423. FILE *procf;
  2424. char buf[100];
  2425. struct stat statbuf;
  2426. int is_ide = 0;
  2427. /* No device was given explicitly, and we are trying some
  2428. likely things. But opening /dev/hdc may produce errors like
  2429. "hdc: tray open or drive not ready"
  2430. if it happens to be a CD-ROM drive. It even happens that
  2431. the process hangs on the attempt to read a music CD.
  2432. So try to be careful. This only works since 2.1.73. */
  2433. if (strncmp("/dev/hd", device, 7))
  2434. return 0;
  2435. snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
  2436. procf = fopen_for_read(buf);
  2437. if (procf != NULL && fgets(buf, sizeof(buf), procf))
  2438. is_ide = (!strncmp(buf, "cdrom", 5) ||
  2439. !strncmp(buf, "tape", 4));
  2440. else
  2441. /* Now when this proc file does not exist, skip the
  2442. device when it is read-only. */
  2443. if (stat(device, &statbuf) == 0)
  2444. is_ide = ((statbuf.st_mode & 0222) == 0);
  2445. if (procf)
  2446. fclose(procf);
  2447. return is_ide;
  2448. }
  2449. static void
  2450. open_list_and_close(const char *device, int user_specified)
  2451. {
  2452. int gb;
  2453. disk_device = device;
  2454. if (setjmp(listingbuf))
  2455. return;
  2456. if (!user_specified)
  2457. if (is_ide_cdrom_or_tape(device))
  2458. return;
  2459. /* Open disk_device, save file descriptor to dev_fd */
  2460. errno = 0;
  2461. gb = get_boot(TRY_ONLY);
  2462. if (gb > 0) { /* I/O error */
  2463. /* Ignore other errors, since we try IDE
  2464. and SCSI hard disks which may not be
  2465. installed on the system. */
  2466. if (user_specified || errno == EACCES)
  2467. bb_perror_msg("can't open '%s'", device);
  2468. return;
  2469. }
  2470. if (gb < 0) { /* no DOS signature */
  2471. list_disk_geometry();
  2472. if (LABEL_IS_AIX)
  2473. goto ret;
  2474. #if ENABLE_FEATURE_OSF_LABEL
  2475. if (bsd_trydev(device) < 0)
  2476. #endif
  2477. printf("Disk %s doesn't contain a valid "
  2478. "partition table\n", device);
  2479. } else {
  2480. list_table(0);
  2481. #if ENABLE_FEATURE_FDISK_WRITABLE
  2482. if (!LABEL_IS_SUN && g_partitions > 4) {
  2483. delete_partition(ext_index);
  2484. }
  2485. #endif
  2486. }
  2487. ret:
  2488. close_dev_fd();
  2489. }
  2490. /* for fdisk -l: try all things in /proc/partitions
  2491. that look like a partition name (do not end in a digit) */
  2492. static void
  2493. list_devs_in_proc_partititons(void)
  2494. {
  2495. FILE *procpt;
  2496. char line[100], ptname[100], devname[120], *s;
  2497. int ma, mi, sz;
  2498. procpt = fopen_or_warn("/proc/partitions", "r");
  2499. while (fgets(line, sizeof(line), procpt)) {
  2500. if (sscanf(line, " %u %u %u %[^\n ]",
  2501. &ma, &mi, &sz, ptname) != 4)
  2502. continue;
  2503. for (s = ptname; *s; s++)
  2504. continue;
  2505. /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
  2506. if (s[-1] >= '1' && s[-1] <= '9')
  2507. continue;
  2508. sprintf(devname, "/dev/%s", ptname);
  2509. open_list_and_close(devname, 0);
  2510. }
  2511. #if ENABLE_FEATURE_CLEAN_UP
  2512. fclose(procpt);
  2513. #endif
  2514. }
  2515. #if ENABLE_FEATURE_FDISK_WRITABLE
  2516. static void
  2517. unknown_command(int c)
  2518. {
  2519. printf("%c: unknown command\n", c);
  2520. }
  2521. #endif
  2522. int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2523. int fdisk_main(int argc UNUSED_PARAM, char **argv)
  2524. {
  2525. unsigned opt;
  2526. /*
  2527. * fdisk -v
  2528. * fdisk -l [-b sectorsize] [-u] device ...
  2529. * fdisk -s [partition] ...
  2530. * fdisk [-b sectorsize] [-u] device
  2531. *
  2532. * Options -C, -H, -S set the geometry.
  2533. */
  2534. INIT_G();
  2535. close_dev_fd(); /* needed: fd 3 must not stay closed */
  2536. opt_complementary = "b+:C+:H+:S+"; /* numeric params */
  2537. opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
  2538. &sector_size, &user_cylinders, &user_heads, &user_sectors);
  2539. argv += optind;
  2540. if (opt & OPT_b) {
  2541. /* Ugly: this sector size is really per device,
  2542. * so cannot be combined with multiple disks,
  2543. * and the same goes for the C/H/S options.
  2544. */
  2545. if (sector_size < 512
  2546. || sector_size > 0x10000
  2547. || (sector_size & (sector_size-1)) /* not power of 2 */
  2548. ) {
  2549. bb_show_usage();
  2550. }
  2551. sector_offset = 2;
  2552. user_set_sector_size = 1;
  2553. }
  2554. if (user_heads <= 0 || user_heads >= 256)
  2555. user_heads = 0;
  2556. if (user_sectors <= 0 || user_sectors >= 64)
  2557. user_sectors = 0;
  2558. if (opt & OPT_u)
  2559. display_in_cyl_units = 0; // -u
  2560. #if ENABLE_FEATURE_FDISK_WRITABLE
  2561. if (opt & OPT_l) {
  2562. nowarn = 1;
  2563. #endif
  2564. if (*argv) {
  2565. listing = 1;
  2566. do {
  2567. open_list_and_close(*argv, 1);
  2568. } while (*++argv);
  2569. } else {
  2570. /* we don't have device names, */
  2571. /* use /proc/partitions instead */
  2572. list_devs_in_proc_partititons();
  2573. }
  2574. return 0;
  2575. #if ENABLE_FEATURE_FDISK_WRITABLE
  2576. }
  2577. #endif
  2578. #if ENABLE_FEATURE_FDISK_BLKSIZE
  2579. if (opt & OPT_s) {
  2580. int j;
  2581. nowarn = 1;
  2582. if (!argv[0])
  2583. bb_show_usage();
  2584. for (j = 0; argv[j]; j++) {
  2585. unsigned long long size;
  2586. fd = xopen(argv[j], O_RDONLY);
  2587. size = bb_BLKGETSIZE_sectors(fd) / 2;
  2588. close(fd);
  2589. if (argv[1])
  2590. printf("%llu\n", size);
  2591. else
  2592. printf("%s: %llu\n", argv[j], size);
  2593. }
  2594. return 0;
  2595. }
  2596. #endif
  2597. #if ENABLE_FEATURE_FDISK_WRITABLE
  2598. if (!argv[0] || argv[1])
  2599. bb_show_usage();
  2600. disk_device = argv[0];
  2601. get_boot(OPEN_MAIN);
  2602. if (LABEL_IS_OSF) {
  2603. /* OSF label, and no DOS label */
  2604. printf("Detected an OSF/1 disklabel on %s, entering "
  2605. "disklabel mode\n", disk_device);
  2606. bsd_select();
  2607. /*Why do we do this? It seems to be counter-intuitive*/
  2608. current_label_type = LABEL_DOS;
  2609. /* If we return we may want to make an empty DOS label? */
  2610. }
  2611. while (1) {
  2612. int c;
  2613. bb_putchar('\n');
  2614. c = 0x20 | read_nonempty("Command (m for help): ");
  2615. switch (c) {
  2616. case 'a':
  2617. if (LABEL_IS_DOS)
  2618. toggle_active(get_partition(1, g_partitions));
  2619. else if (LABEL_IS_SUN)
  2620. toggle_sunflags(get_partition(1, g_partitions),
  2621. 0x01);
  2622. else if (LABEL_IS_SGI)
  2623. sgi_set_bootpartition(
  2624. get_partition(1, g_partitions));
  2625. else
  2626. unknown_command(c);
  2627. break;
  2628. case 'b':
  2629. if (LABEL_IS_SGI) {
  2630. printf("\nThe current boot file is: %s\n",
  2631. sgi_get_bootfile());
  2632. if (read_maybe_empty("Please enter the name of the "
  2633. "new boot file: ") == '\n')
  2634. printf("Boot file unchanged\n");
  2635. else
  2636. sgi_set_bootfile(line_ptr);
  2637. }
  2638. #if ENABLE_FEATURE_OSF_LABEL
  2639. else
  2640. bsd_select();
  2641. #endif
  2642. break;
  2643. case 'c':
  2644. if (LABEL_IS_DOS)
  2645. toggle_dos_compatibility_flag();
  2646. else if (LABEL_IS_SUN)
  2647. toggle_sunflags(get_partition(1, g_partitions),
  2648. 0x10);
  2649. else if (LABEL_IS_SGI)
  2650. sgi_set_swappartition(
  2651. get_partition(1, g_partitions));
  2652. else
  2653. unknown_command(c);
  2654. break;
  2655. case 'd':
  2656. {
  2657. int j;
  2658. /* If sgi_label then don't use get_existing_partition,
  2659. let the user select a partition, since
  2660. get_existing_partition() only works for Linux-like
  2661. partition tables */
  2662. if (!LABEL_IS_SGI) {
  2663. j = get_existing_partition(1, g_partitions);
  2664. } else {
  2665. j = get_partition(1, g_partitions);
  2666. }
  2667. if (j >= 0)
  2668. delete_partition(j);
  2669. }
  2670. break;
  2671. case 'i':
  2672. if (LABEL_IS_SGI)
  2673. create_sgiinfo();
  2674. else
  2675. unknown_command(c);
  2676. case 'l':
  2677. list_types(get_sys_types());
  2678. break;
  2679. case 'm':
  2680. menu();
  2681. break;
  2682. case 'n':
  2683. new_partition();
  2684. break;
  2685. case 'o':
  2686. create_doslabel();
  2687. break;
  2688. case 'p':
  2689. list_table(0);
  2690. break;
  2691. case 'q':
  2692. if (ENABLE_FEATURE_CLEAN_UP)
  2693. close_dev_fd();
  2694. bb_putchar('\n');
  2695. return 0;
  2696. case 's':
  2697. #if ENABLE_FEATURE_SUN_LABEL
  2698. create_sunlabel();
  2699. #endif
  2700. break;
  2701. case 't':
  2702. change_sysid();
  2703. break;
  2704. case 'u':
  2705. change_units();
  2706. break;
  2707. case 'v':
  2708. verify();
  2709. break;
  2710. case 'w':
  2711. write_table(); /* does not return */
  2712. break;
  2713. #if ENABLE_FEATURE_FDISK_ADVANCED
  2714. case 'x':
  2715. if (LABEL_IS_SGI) {
  2716. printf("\n\tSorry, no experts menu for SGI "
  2717. "partition tables available\n\n");
  2718. } else
  2719. xselect();
  2720. break;
  2721. #endif
  2722. default:
  2723. unknown_command(c);
  2724. menu();
  2725. }
  2726. }
  2727. return 0;
  2728. #endif /* FEATURE_FDISK_WRITABLE */
  2729. }