fdisk.c 74 KB

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