fdisk.c 81 KB

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