fdisk.c 80 KB

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