fdisk.c 73 KB

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