fdisk.c 71 KB

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