fdisk.c 73 KB

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