fdisk.c 81 KB

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