]>
Commit | Line | Data |
---|---|---|
e2ee9178 | 1 | /* |
c10937dc | 2 | * |
50992267 KZ |
3 | * Copyright (C) 2007-2013 Karel Zak <kzak@redhat.com> |
4 | * 2012 Davidlohr Bueso <dave@gnu.org> | |
c10937dc KZ |
5 | * |
6 | * This is re-written version for libfdisk, the original was fdiskdoslabel.c | |
7 | * from util-linux fdisk. | |
e2ee9178 | 8 | */ |
ddec0e40 | 9 | #include "c.h" |
e2ee9178 | 10 | #include "nls.h" |
e2ee9178 | 11 | #include "randutils.h" |
bb58112e | 12 | #include "pt-mbr.h" |
ac1a559a | 13 | #include "strutils.h" |
bb58112e | 14 | |
c10937dc KZ |
15 | #include "fdiskP.h" |
16 | ||
235adaa6 KZ |
17 | #include <ctype.h> |
18 | ||
bb58112e | 19 | #define MAXIMUM_PARTS 60 |
bb58112e KZ |
20 | #define ACTIVE_FLAG 0x80 |
21 | ||
b7d101a2 KZ |
22 | /** |
23 | * SECTION: dos | |
705854f3 KZ |
24 | * @title: DOS (MBR) |
25 | * @short_description: disk label specific functions | |
b7d101a2 KZ |
26 | * |
27 | */ | |
28 | ||
29 | ||
bb58112e KZ |
30 | #define IS_EXTENDED(i) \ |
31 | ((i) == MBR_DOS_EXTENDED_PARTITION \ | |
32 | || (i) == MBR_W95_EXTENDED_PARTITION \ | |
33 | || (i) == MBR_LINUX_EXTENDED_PARTITION) | |
34 | ||
af0df606 KZ |
35 | /* |
36 | * per partition table entry data | |
37 | * | |
38 | * The four primary partitions have the same sectorbuffer | |
39 | * and have NULL ex_entry. | |
40 | * | |
41 | * Each logical partition table entry has two pointers, one for the | |
42 | * partition and one link to the next one. | |
43 | */ | |
44 | struct pte { | |
45 | struct dos_partition *pt_entry; /* on-disk MBR entry */ | |
46 | struct dos_partition *ex_entry; /* on-disk EBR entry */ | |
0073a4cf | 47 | fdisk_sector_t offset; /* disk sector number */ |
af0df606 | 48 | unsigned char *sectorbuffer; /* disk sector contents */ |
ddec0e40 KZ |
49 | |
50 | unsigned int changed : 1, | |
51 | private_sectorbuffer : 1; | |
af0df606 | 52 | }; |
0c5d095e KZ |
53 | |
54 | /* | |
55 | * in-memory fdisk GPT stuff | |
56 | */ | |
57 | struct fdisk_dos_label { | |
58 | struct fdisk_label head; /* generic part */ | |
852ce62b | 59 | |
af0df606 | 60 | struct pte ptes[MAXIMUM_PARTS]; /* partition */ |
0073a4cf | 61 | fdisk_sector_t ext_offset; /* start of the ext.partition */ |
e77313a6 | 62 | size_t ext_index; /* ext.partition index (if ext_offset is set) */ |
8b3d9241 KZ |
63 | unsigned int compatible : 1, /* is DOS compatible? */ |
64 | non_pt_changed : 1; /* MBR, but no PT changed */ | |
0c5d095e KZ |
65 | }; |
66 | ||
67 | /* | |
68 | * Partition types | |
69 | */ | |
749af4b6 | 70 | static struct fdisk_parttype dos_parttypes[] = { |
50992267 | 71 | #include "pt-mbr-partnames.h" |
749af4b6 KZ |
72 | }; |
73 | ||
9dea2923 | 74 | #define set_hsc(h,s,c,sector) { \ |
24cd580b DB |
75 | s = sector % cxt->geom.sectors + 1; \ |
76 | sector /= cxt->geom.sectors; \ | |
77 | h = sector % cxt->geom.heads; \ | |
78 | sector /= cxt->geom.heads; \ | |
79 | c = sector & 0xff; \ | |
80 | s |= (sector >> 2) & 0xc0; \ | |
9dea2923 DB |
81 | } |
82 | ||
5dfca634 KZ |
83 | |
84 | #define sector(s) ((s) & 0x3f) | |
85 | #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2)) | |
86 | ||
cf3808e4 | 87 | #define alignment_required(_x) ((_x)->grain != (_x)->sector_size) |
9dea2923 | 88 | |
28b85e60 | 89 | #define is_dos_compatible(_x) \ |
aa36c2cf | 90 | (fdisk_is_label(_x, DOS) && \ |
6a632136 | 91 | fdisk_dos_is_compatible(fdisk_get_label(_x, NULL))) |
96f817fb | 92 | |
80063996 | 93 | #define cround(c, n) fdisk_cround(c, n) |
3c88fb56 | 94 | |
af0df606 KZ |
95 | |
96 | static inline struct fdisk_dos_label *self_label(struct fdisk_context *cxt) | |
97 | { | |
98 | assert(cxt); | |
99 | assert(cxt->label); | |
aa36c2cf | 100 | assert(fdisk_is_label(cxt, DOS)); |
af0df606 KZ |
101 | |
102 | return (struct fdisk_dos_label *) cxt->label; | |
103 | } | |
104 | ||
105 | static inline struct pte *self_pte(struct fdisk_context *cxt, size_t i) | |
106 | { | |
107 | struct fdisk_dos_label *l = self_label(cxt); | |
108 | ||
109 | if (i >= ARRAY_SIZE(l->ptes)) | |
110 | return NULL; | |
111 | ||
112 | return &l->ptes[i]; | |
113 | } | |
114 | ||
115 | static inline struct dos_partition *self_partition( | |
116 | struct fdisk_context *cxt, | |
117 | size_t i) | |
118 | { | |
119 | struct pte *pe = self_pte(cxt, i); | |
120 | return pe ? pe->pt_entry : NULL; | |
121 | } | |
122 | ||
123 | struct dos_partition *fdisk_dos_get_partition( | |
124 | struct fdisk_context *cxt, | |
125 | size_t i) | |
126 | { | |
60c1b036 KZ |
127 | assert(cxt); |
128 | assert(cxt->label); | |
aa36c2cf | 129 | assert(fdisk_is_label(cxt, DOS)); |
60c1b036 | 130 | |
af0df606 KZ |
131 | return self_partition(cxt, i); |
132 | } | |
133 | ||
7f01ec7a KZ |
134 | static struct fdisk_parttype *dos_partition_parttype( |
135 | struct fdisk_context *cxt, | |
136 | struct dos_partition *p) | |
137 | { | |
138 | struct fdisk_parttype *t | |
a745611d | 139 | = fdisk_label_get_parttype_from_code(cxt->label, p->sys_ind); |
7f01ec7a KZ |
140 | return t ? : fdisk_new_unknown_parttype(p->sys_ind, NULL); |
141 | } | |
142 | ||
1b92d076 KZ |
143 | /* |
144 | * Linux kernel cares about partition size only. Things like | |
145 | * partition type or so are completely irrelevant -- kzak Nov-2013 | |
146 | */ | |
147 | static int is_used_partition(struct dos_partition *p) | |
148 | { | |
149 | return p && dos_partition_get_size(p) != 0; | |
150 | } | |
151 | ||
af0df606 KZ |
152 | static void partition_set_changed( |
153 | struct fdisk_context *cxt, | |
154 | size_t i, | |
155 | int changed) | |
156 | { | |
157 | struct pte *pe = self_pte(cxt, i); | |
158 | ||
159 | if (!pe) | |
160 | return; | |
161 | ||
88141067 | 162 | DBG(LABEL, ul_debug("DOS: setting %zu partition to %s", i, |
afef428f | 163 | changed ? "changed" : "unchanged")); |
9c825fa0 | 164 | |
ddec0e40 | 165 | pe->changed = changed ? 1 : 0; |
af0df606 KZ |
166 | if (changed) |
167 | fdisk_label_set_changed(cxt->label, 1); | |
168 | } | |
169 | ||
0073a4cf | 170 | static fdisk_sector_t get_abs_partition_start(struct pte *pe) |
61eaad50 | 171 | { |
60c1b036 KZ |
172 | assert(pe); |
173 | assert(pe->pt_entry); | |
174 | ||
61eaad50 KZ |
175 | return pe->offset + dos_partition_get_start(pe->pt_entry); |
176 | } | |
177 | ||
0073a4cf | 178 | static fdisk_sector_t get_abs_partition_end(struct pte *pe) |
d0a870c5 | 179 | { |
0073a4cf | 180 | fdisk_sector_t size; |
1b92d076 KZ |
181 | |
182 | assert(pe); | |
183 | assert(pe->pt_entry); | |
184 | ||
185 | size = dos_partition_get_size(pe->pt_entry); | |
186 | return get_abs_partition_start(pe) + size - (size ? 1 : 0); | |
d0a870c5 KZ |
187 | } |
188 | ||
a64aa3b8 KZ |
189 | static int is_cleared_partition(struct dos_partition *p) |
190 | { | |
191 | return !(!p || p->boot_ind || p->bh || p->bs || p->bc || | |
192 | p->sys_ind || p->eh || p->es || p->ec || | |
193 | dos_partition_get_start(p) || dos_partition_get_size(p)); | |
194 | } | |
195 | ||
77d6a70a | 196 | static int get_partition_unused_primary(struct fdisk_context *cxt, |
44374b10 BE |
197 | struct fdisk_partition *pa, |
198 | size_t *partno) | |
9dea2923 | 199 | { |
44374b10 | 200 | size_t org, n; |
1bf3a034 | 201 | int rc; |
9dea2923 | 202 | |
44374b10 BE |
203 | assert(cxt); |
204 | assert(cxt->label); | |
205 | assert(partno); | |
206 | ||
207 | org = cxt->label->nparts_max; | |
208 | ||
1bf3a034 | 209 | cxt->label->nparts_max = 4; |
6c89f750 | 210 | rc = fdisk_partition_next_partno(pa, cxt, &n); |
77d6a70a | 211 | cxt->label->nparts_max = org; |
1bf3a034 | 212 | |
44374b10 | 213 | if (rc == 1) { |
ac1a559a | 214 | fdisk_info(cxt, _("All primary partitions have been defined already.")); |
44374b10 BE |
215 | rc = -1; |
216 | } else if (rc == 0) | |
217 | *partno = n; | |
218 | return rc; | |
9dea2923 DB |
219 | } |
220 | ||
0073a4cf | 221 | static int seek_sector(struct fdisk_context *cxt, fdisk_sector_t secno) |
c37ebc12 KZ |
222 | { |
223 | off_t offset = (off_t) secno * cxt->sector_size; | |
224 | ||
225 | return lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1 ? -errno : 0; | |
226 | } | |
227 | ||
0073a4cf | 228 | static int read_sector(struct fdisk_context *cxt, fdisk_sector_t secno, |
c37ebc12 KZ |
229 | unsigned char *buf) |
230 | { | |
231 | int rc = seek_sector(cxt, secno); | |
11fe0448 | 232 | ssize_t r; |
c37ebc12 KZ |
233 | |
234 | if (rc < 0) | |
235 | return rc; | |
236 | ||
11fe0448 KZ |
237 | r = read(cxt->dev_fd, buf, cxt->sector_size); |
238 | if (r == (ssize_t) cxt->sector_size) | |
239 | return 0; | |
240 | if (r < 0) | |
241 | return -errno; | |
242 | return -1; | |
c37ebc12 | 243 | } |
9dea2923 | 244 | |
e2ee9178 | 245 | /* Allocate a buffer and read a partition table sector */ |
0073a4cf | 246 | static int read_pte(struct fdisk_context *cxt, size_t pno, fdisk_sector_t offset) |
e2ee9178 | 247 | { |
11fe0448 | 248 | int rc; |
af0df606 KZ |
249 | unsigned char *buf; |
250 | struct pte *pe = self_pte(cxt, pno); | |
251 | ||
252 | buf = calloc(1, cxt->sector_size); | |
253 | if (!buf) | |
254 | return -ENOMEM; | |
e2ee9178 | 255 | |
613596b3 KZ |
256 | DBG(LABEL, ul_debug("DOS: reading EBR %zu: offset=%ju, buffer=%p", |
257 | pno, (uintmax_t) offset, buf)); | |
9c825fa0 | 258 | |
e2ee9178 | 259 | pe->offset = offset; |
af0df606 | 260 | pe->sectorbuffer = buf; |
ddec0e40 | 261 | pe->private_sectorbuffer = 1; |
21770662 | 262 | |
11fe0448 KZ |
263 | rc = read_sector(cxt, offset, pe->sectorbuffer); |
264 | if (rc) { | |
fcea064b | 265 | fdisk_warn(cxt, _("Failed to read extended partition table " |
829f4206 | 266 | "(offset=%ju)"), (uintmax_t) offset); |
11fe0448 KZ |
267 | return rc; |
268 | } | |
269 | ||
e2ee9178 | 270 | pe->changed = 0; |
6864f168 | 271 | pe->pt_entry = pe->ex_entry = NULL; |
af0df606 | 272 | return 0; |
e2ee9178 DB |
273 | } |
274 | ||
e2ee9178 | 275 | |
f540fe15 | 276 | static void clear_partition(struct dos_partition *p) |
e2ee9178 DB |
277 | { |
278 | if (!p) | |
279 | return; | |
280 | p->boot_ind = 0; | |
da967236 KZ |
281 | p->bh = 0; |
282 | p->bs = 0; | |
283 | p->bc = 0; | |
e2ee9178 | 284 | p->sys_ind = 0; |
da967236 KZ |
285 | p->eh = 0; |
286 | p->es = 0; | |
287 | p->ec = 0; | |
b44244cb KZ |
288 | dos_partition_set_start(p,0); |
289 | dos_partition_set_size(p,0); | |
e2ee9178 DB |
290 | } |
291 | ||
ddec0e40 | 292 | static void dos_init(struct fdisk_context *cxt) |
e2ee9178 | 293 | { |
fcea064b | 294 | struct fdisk_dos_label *l = self_label(cxt); |
e3661531 | 295 | size_t i; |
e2ee9178 | 296 | |
60c1b036 KZ |
297 | assert(cxt); |
298 | assert(cxt->label); | |
aa36c2cf | 299 | assert(fdisk_is_label(cxt, DOS)); |
60c1b036 | 300 | |
88141067 | 301 | DBG(LABEL, ul_debug("DOS: initialize, first sector buffer %p", cxt->firstsector)); |
9c825fa0 | 302 | |
e3661531 | 303 | cxt->label->nparts_max = 4; /* default, unlimited number of logical */ |
ddec0e40 | 304 | |
fcea064b KZ |
305 | l->ext_index = 0; |
306 | l->ext_offset = 0; | |
ddec0e40 KZ |
307 | l->non_pt_changed = 0; |
308 | ||
309 | memset(l->ptes, 0, sizeof(l->ptes)); | |
e2ee9178 DB |
310 | |
311 | for (i = 0; i < 4; i++) { | |
af0df606 | 312 | struct pte *pe = self_pte(cxt, i); |
e2ee9178 | 313 | |
e8bf93de | 314 | pe->pt_entry = mbr_get_partition(cxt->firstsector, i); |
6864f168 | 315 | pe->ex_entry = NULL; |
e2ee9178 | 316 | pe->offset = 0; |
67987b47 | 317 | pe->sectorbuffer = cxt->firstsector; |
9c825fa0 | 318 | pe->private_sectorbuffer = 0; |
e2ee9178 DB |
319 | pe->changed = 0; |
320 | } | |
321 | ||
6a632136 | 322 | if (fdisk_is_listonly(cxt)) |
4f79f887 KZ |
323 | return; |
324 | /* | |
325 | * Various warnings... | |
326 | */ | |
327 | if (fdisk_missing_geometry(cxt)) | |
328 | fdisk_warnx(cxt, _("You can set geometry from the extra functions menu.")); | |
329 | ||
330 | if (is_dos_compatible(cxt)) { | |
331 | fdisk_warnx(cxt, _("DOS-compatible mode is deprecated.")); | |
332 | ||
333 | if (cxt->sector_size != cxt->phy_sector_size) | |
334 | fdisk_info(cxt, _( | |
335 | "The device presents a logical sector size that is smaller than " | |
336 | "the physical sector size. Aligning to a physical sector (or optimal " | |
337 | "I/O) size boundary is recommended, or performance may be impacted.")); | |
338 | } | |
339 | ||
6a632136 | 340 | if (fdisk_use_cylinders(cxt)) |
4f79f887 | 341 | fdisk_warnx(cxt, _("Cylinders as display units are deprecated.")); |
0b52b94c | 342 | |
4f79f887 | 343 | if (cxt->total_sectors > UINT_MAX) { |
ac1a559a KZ |
344 | uint64_t bytes = cxt->total_sectors * cxt->sector_size; |
345 | char *szstr = size_to_human_string(SIZE_SUFFIX_SPACE | |
346 | | SIZE_SUFFIX_3LETTER, bytes); | |
fcea064b | 347 | fdisk_warnx(cxt, |
829f4206 | 348 | _("The size of this disk is %s (%ju bytes). DOS " |
fcea064b | 349 | "partition table format can not be used on drives for " |
27aadd8b | 350 | "volumes larger than %lu bytes for %lu-byte " |
fcea064b | 351 | "sectors. Use GUID partition table format (GPT)."), |
829f4206 KZ |
352 | szstr, bytes, |
353 | UINT_MAX * cxt->sector_size, | |
0b52b94c | 354 | cxt->sector_size); |
ac1a559a | 355 | free(szstr); |
0b52b94c | 356 | } |
e2ee9178 DB |
357 | } |
358 | ||
ddec0e40 KZ |
359 | /* callback called by libfdisk */ |
360 | static void dos_deinit(struct fdisk_label *lb) | |
361 | { | |
362 | size_t i; | |
363 | struct fdisk_dos_label *l = (struct fdisk_dos_label *) lb; | |
364 | ||
365 | for (i = 0; i < ARRAY_SIZE(l->ptes); i++) { | |
366 | struct pte *pe = &l->ptes[i]; | |
367 | ||
9c825fa0 | 368 | if (pe->private_sectorbuffer && pe->sectorbuffer) { |
88141067 | 369 | DBG(LABEL, ul_debug("DOS: freeing pte %zu sector buffer %p", |
9c825fa0 | 370 | i, pe->sectorbuffer)); |
ddec0e40 | 371 | free(pe->sectorbuffer); |
9c825fa0 | 372 | } |
ddec0e40 | 373 | pe->sectorbuffer = NULL; |
9c825fa0 | 374 | pe->private_sectorbuffer = 0; |
ddec0e40 KZ |
375 | } |
376 | ||
377 | memset(l->ptes, 0, sizeof(l->ptes)); | |
378 | } | |
379 | ||
613596b3 KZ |
380 | static void reset_pte(struct pte *pe) |
381 | { | |
382 | assert(pe); | |
383 | ||
384 | if (pe->private_sectorbuffer) { | |
385 | DBG(LABEL, ul_debug(" --> freeing pte sector buffer %p", | |
386 | pe->sectorbuffer)); | |
387 | free(pe->sectorbuffer); | |
388 | } | |
389 | memset(pe, 0, sizeof(struct pte)); | |
390 | } | |
391 | ||
9ffeb235 | 392 | static int dos_delete_partition(struct fdisk_context *cxt, size_t partnum) |
61c4cb85 | 393 | { |
60c1b036 KZ |
394 | struct fdisk_dos_label *l; |
395 | struct pte *pe; | |
af0df606 KZ |
396 | struct dos_partition *p; |
397 | struct dos_partition *q; | |
398 | ||
60c1b036 KZ |
399 | assert(cxt); |
400 | assert(cxt->label); | |
aa36c2cf | 401 | assert(fdisk_is_label(cxt, DOS)); |
60c1b036 KZ |
402 | |
403 | pe = self_pte(cxt, partnum); | |
af0df606 KZ |
404 | if (!pe) |
405 | return -EINVAL; | |
406 | ||
88141067 | 407 | DBG(LABEL, ul_debug("DOS: delete partiton %zu (max=%zu)", partnum, |
bcddbe96 | 408 | cxt->label->nparts_max)); |
9c825fa0 | 409 | |
60c1b036 | 410 | l = self_label(cxt); |
af0df606 KZ |
411 | p = pe->pt_entry; |
412 | q = pe->ex_entry; | |
61c4cb85 DB |
413 | |
414 | /* Note that for the fifth partition (partnum == 4) we don't actually | |
415 | decrement partitions. */ | |
61c4cb85 | 416 | if (partnum < 4) { |
88141067 | 417 | DBG(LABEL, ul_debug("--> delete primary")); |
fcea064b | 418 | if (IS_EXTENDED(p->sys_ind) && partnum == l->ext_index) { |
e3661531 | 419 | cxt->label->nparts_max = 4; |
fcea064b KZ |
420 | l->ptes[l->ext_index].ex_entry = NULL; |
421 | l->ext_offset = 0; | |
e77313a6 | 422 | l->ext_index = 0; |
61c4cb85 | 423 | } |
af0df606 | 424 | partition_set_changed(cxt, partnum, 1); |
61c4cb85 DB |
425 | clear_partition(p); |
426 | } else if (!q->sys_ind && partnum > 4) { | |
88141067 | 427 | DBG(LABEL, ul_debug("--> delete logical [last in the chain]")); |
613596b3 | 428 | reset_pte(&l->ptes[partnum]); |
e3661531 | 429 | --cxt->label->nparts_max; |
61c4cb85 | 430 | --partnum; |
613596b3 | 431 | /* clear link to deleted partition */ |
af0df606 KZ |
432 | clear_partition(l->ptes[partnum].ex_entry); |
433 | partition_set_changed(cxt, partnum, 1); | |
61c4cb85 | 434 | } else { |
88141067 | 435 | DBG(LABEL, ul_debug("--> delete logical [move down]")); |
61c4cb85 | 436 | if (partnum > 4) { |
88141067 | 437 | DBG(LABEL, ul_debug(" --> delete %zu logical link", partnum)); |
af0df606 | 438 | p = l->ptes[partnum - 1].ex_entry; |
61c4cb85 | 439 | *p = *q; |
b44244cb KZ |
440 | dos_partition_set_start(p, dos_partition_get_start(q)); |
441 | dos_partition_set_size(p, dos_partition_get_size(q)); | |
af0df606 | 442 | partition_set_changed(cxt, partnum - 1, 1); |
61c4cb85 | 443 | |
bcddbe96 | 444 | } else if (cxt->label->nparts_max > 5) { |
88141067 | 445 | DBG(LABEL, ul_debug(" --> delete first logical link")); |
bcddbe96 KZ |
446 | pe = &l->ptes[5]; /* second logical */ |
447 | ||
448 | if (pe->pt_entry) /* prevent SEGFAULT */ | |
af0df606 KZ |
449 | dos_partition_set_start(pe->pt_entry, |
450 | get_abs_partition_start(pe) - | |
fcea064b KZ |
451 | l->ext_offset); |
452 | pe->offset = l->ext_offset; | |
af0df606 | 453 | partition_set_changed(cxt, 5, 1); |
61c4cb85 DB |
454 | } |
455 | ||
e3661531 | 456 | if (cxt->label->nparts_max > 5) { |
88141067 | 457 | DBG(LABEL, ul_debug(" --> move ptes")); |
e3661531 | 458 | cxt->label->nparts_max--; |
613596b3 | 459 | reset_pte(&l->ptes[partnum]); |
e3661531 | 460 | while (partnum < cxt->label->nparts_max) { |
88141067 | 461 | DBG(LABEL, ul_debug(" --> moving pte %zu <-- %zu", partnum, partnum + 1)); |
af0df606 | 462 | l->ptes[partnum] = l->ptes[partnum + 1]; |
61c4cb85 DB |
463 | partnum++; |
464 | } | |
9c825fa0 | 465 | memset(&l->ptes[partnum], 0, sizeof(struct pte)); |
bcddbe96 | 466 | } else { |
88141067 | 467 | DBG(LABEL, ul_debug(" --> the only logical: clear only")); |
af0df606 | 468 | clear_partition(l->ptes[partnum].pt_entry); |
bcddbe96 KZ |
469 | cxt->label->nparts_max--; |
470 | ||
471 | if (partnum == 4) { | |
88141067 | 472 | DBG(LABEL, ul_debug(" --> clear last logical")); |
613596b3 | 473 | reset_pte(&l->ptes[partnum]); |
bcddbe96 KZ |
474 | partition_set_changed(cxt, l->ext_index, 1); |
475 | } | |
476 | } | |
61c4cb85 | 477 | } |
1f5eb51b | 478 | |
9ffeb235 | 479 | fdisk_label_set_changed(cxt->label, 1); |
1f5eb51b | 480 | return 0; |
61c4cb85 DB |
481 | } |
482 | ||
829f4206 | 483 | static void read_extended(struct fdisk_context *cxt, size_t ext) |
e2ee9178 | 484 | { |
e3661531 | 485 | size_t i; |
613596b3 | 486 | struct pte *pex, *pe; |
f540fe15 | 487 | struct dos_partition *p, *q; |
fcea064b | 488 | struct fdisk_dos_label *l = self_label(cxt); |
e2ee9178 | 489 | |
fcea064b | 490 | l->ext_index = ext; |
af0df606 | 491 | pex = self_pte(cxt, ext); |
6864f168 | 492 | pex->ex_entry = pex->pt_entry; |
e2ee9178 | 493 | |
6864f168 | 494 | p = pex->pt_entry; |
b44244cb | 495 | if (!dos_partition_get_start(p)) { |
ac1a559a | 496 | fdisk_warnx(cxt, _("Bad offset in primary extended partition.")); |
e2ee9178 DB |
497 | return; |
498 | } | |
499 | ||
88141067 | 500 | DBG(LABEL, ul_debug("DOS: Reading extended %zu", ext)); |
9c825fa0 | 501 | |
e2ee9178 | 502 | while (IS_EXTENDED (p->sys_ind)) { |
613596b3 | 503 | pe = self_pte(cxt, cxt->label->nparts_max); |
e2ee9178 | 504 | |
e3661531 | 505 | if (cxt->label->nparts_max >= MAXIMUM_PARTS) { |
e2ee9178 DB |
506 | /* This is not a Linux restriction, but |
507 | this program uses arrays of size MAXIMUM_PARTS. | |
508 | Do not try to `improve' this test. */ | |
af0df606 KZ |
509 | struct pte *pre = self_pte(cxt, |
510 | cxt->label->nparts_max - 1); | |
fcea064b | 511 | fdisk_warnx(cxt, |
829f4206 | 512 | _("Omitting partitions after #%zu. They will be deleted " |
fcea064b | 513 | "if you save this partition table."), |
e3661531 | 514 | cxt->label->nparts_max); |
fcea064b | 515 | |
6864f168 | 516 | clear_partition(pre->ex_entry); |
af0df606 KZ |
517 | partition_set_changed(cxt, |
518 | cxt->label->nparts_max - 1, 1); | |
e2ee9178 DB |
519 | return; |
520 | } | |
521 | ||
11fe0448 KZ |
522 | if (read_pte(cxt, cxt->label->nparts_max, l->ext_offset + |
523 | dos_partition_get_start(p))) | |
524 | return; | |
e2ee9178 | 525 | |
fcea064b KZ |
526 | if (!l->ext_offset) |
527 | l->ext_offset = dos_partition_get_start(p); | |
e2ee9178 | 528 | |
613596b3 | 529 | assert(pe->sectorbuffer); |
e8bf93de | 530 | q = p = mbr_get_partition(pe->sectorbuffer, 0); |
fcea064b | 531 | |
488cc17a KZ |
532 | for (i = 0; i < 4; i++, p++) { |
533 | if (!dos_partition_get_size(p)) | |
534 | continue; | |
535 | ||
e2ee9178 | 536 | if (IS_EXTENDED (p->sys_ind)) { |
6864f168 | 537 | if (pe->ex_entry) |
fcea064b KZ |
538 | fdisk_warnx(cxt, _( |
539 | "Extra link pointer in partition " | |
829f4206 | 540 | "table %zu."), |
fcea064b | 541 | cxt->label->nparts_max + 1); |
e2ee9178 | 542 | else |
6864f168 | 543 | pe->ex_entry = p; |
e2ee9178 | 544 | } else if (p->sys_ind) { |
6864f168 | 545 | if (pe->pt_entry) |
fcea064b KZ |
546 | fdisk_warnx(cxt, _( |
547 | "Ignoring extra data in partition " | |
829f4206 | 548 | "table %zu."), |
fcea064b | 549 | cxt->label->nparts_max + 1); |
e2ee9178 | 550 | else |
6864f168 | 551 | pe->pt_entry = p; |
e2ee9178 DB |
552 | } |
553 | } | |
554 | ||
555 | /* very strange code here... */ | |
6864f168 KZ |
556 | if (!pe->pt_entry) { |
557 | if (q != pe->ex_entry) | |
558 | pe->pt_entry = q; | |
e2ee9178 | 559 | else |
6864f168 | 560 | pe->pt_entry = q + 1; |
e2ee9178 | 561 | } |
6864f168 KZ |
562 | if (!pe->ex_entry) { |
563 | if (q != pe->pt_entry) | |
564 | pe->ex_entry = q; | |
e2ee9178 | 565 | else |
6864f168 | 566 | pe->ex_entry = q + 1; |
e2ee9178 DB |
567 | } |
568 | ||
6864f168 | 569 | p = pe->ex_entry; |
e3661531 | 570 | cxt->label->nparts_cur = ++cxt->label->nparts_max; |
613596b3 KZ |
571 | |
572 | DBG(LABEL, ul_debug("DOS: EBR[offset=%ju]: link: type=%x, start=%u, size=%u; " | |
573 | " data: type=%x, start=%u, size=%u", | |
574 | (uintmax_t) pe->offset, | |
575 | pe->ex_entry->sys_ind, | |
576 | dos_partition_get_start(pe->ex_entry), | |
577 | dos_partition_get_size(pe->ex_entry), | |
578 | pe->pt_entry->sys_ind, | |
579 | dos_partition_get_start(pe->pt_entry), | |
580 | dos_partition_get_size(pe->pt_entry))); | |
581 | ||
582 | } | |
583 | ||
584 | /* remove last empty EBR */ | |
585 | pe = self_pte(cxt, cxt->label->nparts_max - 1); | |
586 | if (is_cleared_partition(pe->ex_entry) && | |
587 | is_cleared_partition(pe->pt_entry)) { | |
588 | DBG(LABEL, ul_debug("DOS: EBR[offset=%ju]: empty, remove", (uintmax_t) pe->offset)); | |
589 | reset_pte(pe); | |
590 | cxt->label->nparts_max--; | |
591 | cxt->label->nparts_cur--; | |
e2ee9178 DB |
592 | } |
593 | ||
594 | /* remove empty links */ | |
595 | remove: | |
af0df606 | 596 | q = self_partition(cxt, 4); |
e3661531 | 597 | for (i = 4; i < cxt->label->nparts_max; i++) { |
af0df606 | 598 | p = self_partition(cxt, i); |
e2ee9178 | 599 | |
af0df606 KZ |
600 | if (!dos_partition_get_size(p) && |
601 | (cxt->label->nparts_max > 5 || q->sys_ind)) { | |
829f4206 | 602 | fdisk_info(cxt, _("omitting empty partition (%zu)"), i+1); |
9ffeb235 | 603 | dos_delete_partition(cxt, i); |
e2ee9178 DB |
604 | goto remove; /* numbering changed */ |
605 | } | |
606 | } | |
613596b3 KZ |
607 | |
608 | DBG(LABEL, ul_debug("DOS: nparts_max: %zu", cxt->label->nparts_max)); | |
e2ee9178 DB |
609 | } |
610 | ||
21fe3dde | 611 | static int dos_get_disklabel_id(struct fdisk_context *cxt, char **id) |
e2ee9178 | 612 | { |
21fe3dde KZ |
613 | unsigned int num; |
614 | ||
615 | assert(cxt); | |
616 | assert(id); | |
617 | assert(cxt->label); | |
aa36c2cf | 618 | assert(fdisk_is_label(cxt, DOS)); |
21fe3dde KZ |
619 | |
620 | num = mbr_get_id(cxt->firstsector); | |
621 | if (asprintf(id, "0x%08x", num) > 0) | |
622 | return 0; | |
623 | ||
624 | return -ENOMEM; | |
e2ee9178 DB |
625 | } |
626 | ||
9ffeb235 | 627 | static int dos_create_disklabel(struct fdisk_context *cxt) |
e2ee9178 | 628 | { |
95085ec5 KZ |
629 | unsigned int id = 0; |
630 | int rc, has_id = 0; | |
e2ee9178 | 631 | |
9ffeb235 KZ |
632 | assert(cxt); |
633 | assert(cxt->label); | |
aa36c2cf | 634 | assert(fdisk_is_label(cxt, DOS)); |
9ffeb235 | 635 | |
88141067 | 636 | DBG(LABEL, ul_debug("DOS: creating new disklabel")); |
9c825fa0 | 637 | |
95085ec5 KZ |
638 | if (cxt->script) { |
639 | char *end = NULL; | |
640 | const char *s = fdisk_script_get_header(cxt->script, "label-id"); | |
641 | ||
642 | if (s) { | |
643 | errno = 0; | |
644 | id = strtol(s, &end, 16); | |
645 | if (!errno && end && s < end) | |
646 | has_id = 1; | |
647 | } | |
648 | } | |
649 | ||
e2ee9178 | 650 | /* random disk signature */ |
95085ec5 KZ |
651 | if (!has_id) |
652 | random_get_bytes(&id, sizeof(id)); | |
e2ee9178 | 653 | |
e53ced85 | 654 | dos_init(cxt); |
7c2cfb18 KZ |
655 | rc = fdisk_init_firstsector_buffer(cxt); |
656 | if (rc) | |
657 | return rc; | |
bddd84e7 | 658 | fdisk_label_set_changed(cxt->label, 1); |
e2ee9178 DB |
659 | |
660 | /* Generate an MBR ID for this disk */ | |
67987b47 | 661 | mbr_set_id(cxt->firstsector, id); |
e2ee9178 DB |
662 | |
663 | /* Put MBR signature */ | |
67987b47 | 664 | mbr_set_magic(cxt->firstsector); |
ac1a559a | 665 | |
0477369a | 666 | fdisk_info(cxt, _("Created a new DOS disklabel with disk " |
ac1a559a | 667 | "identifier 0x%08x."), id); |
a71601af | 668 | return 0; |
e2ee9178 DB |
669 | } |
670 | ||
35b1f0a4 | 671 | static int dos_set_disklabel_id(struct fdisk_context *cxt) |
e2ee9178 | 672 | { |
0a49e5d5 KZ |
673 | char *end = NULL, *str = NULL; |
674 | unsigned int id, old; | |
35b1f0a4 | 675 | struct fdisk_dos_label *l; |
0a49e5d5 | 676 | int rc; |
e2ee9178 | 677 | |
35b1f0a4 KZ |
678 | assert(cxt); |
679 | assert(cxt->label); | |
aa36c2cf | 680 | assert(fdisk_is_label(cxt, DOS)); |
35b1f0a4 | 681 | |
88141067 | 682 | DBG(LABEL, ul_debug("DOS: setting Id")); |
9c825fa0 | 683 | |
35b1f0a4 | 684 | l = self_label(cxt); |
0a49e5d5 KZ |
685 | old = mbr_get_id(cxt->firstsector); |
686 | rc = fdisk_ask_string(cxt, | |
a0862e26 | 687 | _("Enter the new disk identifier"), &str); |
0a49e5d5 KZ |
688 | if (rc) |
689 | return rc; | |
e2ee9178 | 690 | |
0a49e5d5 KZ |
691 | errno = 0; |
692 | id = strtoul(str, &end, 0); | |
693 | if (errno || str == end || (end && *end)) { | |
694 | fdisk_warnx(cxt, _("Incorrect value.")); | |
695 | return -EINVAL; | |
696 | } | |
e2ee9178 | 697 | |
e2ee9178 | 698 | |
0a49e5d5 | 699 | mbr_set_id(cxt->firstsector, id); |
8b3d9241 | 700 | l->non_pt_changed = 1; |
bddd84e7 | 701 | fdisk_label_set_changed(cxt->label, 1); |
ac1a559a | 702 | |
0477369a | 703 | fdisk_info(cxt, _("Disk identifier changed from 0x%08x to 0x%08x."), |
ac1a559a | 704 | old, id); |
0a49e5d5 | 705 | return 0; |
e2ee9178 DB |
706 | } |
707 | ||
9a5e29e9 KZ |
708 | static void get_partition_table_geometry(struct fdisk_context *cxt, |
709 | unsigned int *ph, unsigned int *ps) | |
710 | { | |
67987b47 | 711 | unsigned char *bufp = cxt->firstsector; |
f540fe15 | 712 | struct dos_partition *p; |
9a5e29e9 KZ |
713 | int i, h, s, hh, ss; |
714 | int first = 1; | |
715 | int bad = 0; | |
716 | ||
717 | hh = ss = 0; | |
60c1b036 | 718 | for (i = 0; i < 4; i++) { |
e8bf93de | 719 | p = mbr_get_partition(bufp, i); |
9a5e29e9 | 720 | if (p->sys_ind != 0) { |
da967236 KZ |
721 | h = p->eh + 1; |
722 | s = (p->es & 077); | |
9a5e29e9 KZ |
723 | if (first) { |
724 | hh = h; | |
725 | ss = s; | |
726 | first = 0; | |
727 | } else if (hh != h || ss != s) | |
728 | bad = 1; | |
729 | } | |
730 | } | |
731 | ||
732 | if (!first && !bad) { | |
733 | *ph = hh; | |
734 | *ps = ss; | |
735 | } | |
cf3808e4 | 736 | |
88141067 | 737 | DBG(LABEL, ul_debug("DOS PT geometry: heads=%u, sectors=%u", *ph, *ps)); |
9a5e29e9 KZ |
738 | } |
739 | ||
9ffeb235 | 740 | static int dos_reset_alignment(struct fdisk_context *cxt) |
cf3808e4 | 741 | { |
9ffeb235 KZ |
742 | assert(cxt); |
743 | assert(cxt->label); | |
aa36c2cf | 744 | assert(fdisk_is_label(cxt, DOS)); |
9ffeb235 | 745 | |
cf3808e4 | 746 | /* overwrite necessary stuff by DOS deprecated stuff */ |
852ce62b | 747 | if (is_dos_compatible(cxt)) { |
88141067 | 748 | DBG(LABEL, ul_debug("DOS: reseting alignemnt for DOS-comaptiblem PT")); |
cf3808e4 KZ |
749 | if (cxt->geom.sectors) |
750 | cxt->first_lba = cxt->geom.sectors; /* usually 63 */ | |
751 | ||
752 | cxt->grain = cxt->sector_size; /* usually 512 */ | |
753 | } | |
cf3808e4 KZ |
754 | |
755 | return 0; | |
756 | } | |
9a5e29e9 | 757 | |
9ed268d0 KZ |
758 | /* TODO: move to include/pt-dos.h and share with libblkid */ |
759 | #define AIX_MAGIC_STRING "\xC9\xC2\xD4\xC1" | |
760 | #define AIX_MAGIC_STRLEN (sizeof(AIX_MAGIC_STRING) - 1) | |
761 | ||
9ffeb235 | 762 | static int dos_probe_label(struct fdisk_context *cxt) |
e2ee9178 | 763 | { |
e3661531 | 764 | size_t i; |
9a5e29e9 | 765 | unsigned int h = 0, s = 0; |
e2ee9178 | 766 | |
e3661531 KZ |
767 | assert(cxt); |
768 | assert(cxt->label); | |
aa36c2cf | 769 | assert(fdisk_is_label(cxt, DOS)); |
e3661531 | 770 | |
9ed268d0 KZ |
771 | /* ignore disks with AIX magic number */ |
772 | if (memcmp(cxt->firstsector, AIX_MAGIC_STRING, AIX_MAGIC_STRLEN) == 0) | |
773 | return 0; | |
774 | ||
67987b47 | 775 | if (!mbr_is_valid_magic(cxt->firstsector)) |
e2ee9178 DB |
776 | return 0; |
777 | ||
e53ced85 | 778 | dos_init(cxt); |
e2ee9178 | 779 | |
9a5e29e9 KZ |
780 | get_partition_table_geometry(cxt, &h, &s); |
781 | if (h && s) { | |
782 | cxt->geom.heads = h; | |
783 | cxt->geom.sectors = s; | |
784 | } | |
785 | ||
e2ee9178 | 786 | for (i = 0; i < 4; i++) { |
af0df606 | 787 | struct pte *pe = self_pte(cxt, i); |
e2ee9178 | 788 | |
d0a870c5 | 789 | if (is_used_partition(pe->pt_entry)) |
e3661531 KZ |
790 | cxt->label->nparts_cur++; |
791 | ||
6864f168 | 792 | if (IS_EXTENDED (pe->pt_entry->sys_ind)) { |
e3661531 | 793 | if (cxt->label->nparts_max != 4) |
fcea064b | 794 | fdisk_warnx(cxt, _( |
829f4206 | 795 | "Ignoring extra extended partition %zu"), |
fcea064b | 796 | i + 1); |
e2ee9178 | 797 | else |
7737f698 | 798 | read_extended(cxt, i); |
e2ee9178 DB |
799 | } |
800 | } | |
801 | ||
e3661531 | 802 | for (i = 3; i < cxt->label->nparts_max; i++) { |
af0df606 | 803 | struct pte *pe = self_pte(cxt, i); |
bcddbe96 | 804 | struct fdisk_dos_label *l = self_label(cxt); |
e2ee9178 | 805 | |
67188340 | 806 | if (!mbr_is_valid_magic(pe->sectorbuffer)) { |
fcea064b | 807 | fdisk_info(cxt, _( |
bcddbe96 KZ |
808 | "Invalid flag 0x%02x%02x of EBR (for partition %zu) will " |
809 | "be corrected by w(rite)."), | |
6fe71269 KZ |
810 | pe->sectorbuffer[510], |
811 | pe->sectorbuffer[511], | |
812 | i + 1); | |
bcddbe96 KZ |
813 | partition_set_changed(cxt, i, 1); |
814 | ||
815 | /* mark also extended as changed to update the first EBR | |
816 | * in situation that there is no logical partitions at all */ | |
817 | partition_set_changed(cxt, l->ext_index, 1); | |
e2ee9178 DB |
818 | } |
819 | } | |
820 | ||
821 | return 1; | |
822 | } | |
823 | ||
e53ced85 | 824 | static void set_partition(struct fdisk_context *cxt, |
0073a4cf KZ |
825 | int i, int doext, fdisk_sector_t start, |
826 | fdisk_sector_t stop, int sysid, int boot) | |
9dea2923 | 827 | { |
af0df606 | 828 | struct pte *pe = self_pte(cxt, i); |
f540fe15 | 829 | struct dos_partition *p; |
0073a4cf | 830 | fdisk_sector_t offset; |
9dea2923 | 831 | |
ecf40cda KZ |
832 | assert(!FDISK_IS_UNDEF(start)); |
833 | assert(!FDISK_IS_UNDEF(stop)); | |
80063996 | 834 | |
9dea2923 | 835 | if (doext) { |
fcea064b | 836 | struct fdisk_dos_label *l = self_label(cxt); |
af0df606 | 837 | p = pe->ex_entry; |
fcea064b | 838 | offset = l->ext_offset; |
9dea2923 | 839 | } else { |
af0df606 KZ |
840 | p = pe->pt_entry; |
841 | offset = pe->offset; | |
9dea2923 | 842 | } |
a60087aa | 843 | |
613596b3 | 844 | DBG(LABEL, ul_debug("DOS: setting partition %d%s, offset=%zu, start=%zu, stop=%zu, sysid=%02x", |
a60087aa | 845 | i, doext ? " [extended]" : "", |
613596b3 KZ |
846 | (size_t) offset, |
847 | (size_t) (start - offset), | |
848 | (size_t) (stop - start + 1), | |
849 | sysid)); | |
a60087aa | 850 | |
50f7514e | 851 | p->boot_ind = boot ? ACTIVE_FLAG : 0; |
9dea2923 | 852 | p->sys_ind = sysid; |
b44244cb KZ |
853 | dos_partition_set_start(p, start - offset); |
854 | dos_partition_set_size(p, stop - start + 1); | |
9dea2923 | 855 | |
852ce62b | 856 | if (is_dos_compatible(cxt) && (start/(cxt->geom.sectors*cxt->geom.heads) > 1023)) |
24cd580b | 857 | start = cxt->geom.heads*cxt->geom.sectors*1024 - 1; |
da967236 | 858 | set_hsc(p->bh, p->bs, p->bc, start); |
852ce62b | 859 | if (is_dos_compatible(cxt) && (stop/(cxt->geom.sectors*cxt->geom.heads) > 1023)) |
24cd580b | 860 | stop = cxt->geom.heads*cxt->geom.sectors*1024 - 1; |
da967236 | 861 | set_hsc(p->eh, p->es, p->ec, stop); |
af0df606 | 862 | partition_set_changed(cxt, i, 1); |
9dea2923 DB |
863 | } |
864 | ||
0073a4cf KZ |
865 | static fdisk_sector_t get_unused_start(struct fdisk_context *cxt, |
866 | int part_n, fdisk_sector_t start, | |
867 | fdisk_sector_t first[], fdisk_sector_t last[]) | |
9dea2923 | 868 | { |
e3661531 | 869 | size_t i; |
9dea2923 | 870 | |
e3661531 | 871 | for (i = 0; i < cxt->label->nparts_max; i++) { |
0073a4cf | 872 | fdisk_sector_t lastplusoff; |
af0df606 | 873 | struct pte *pe = self_pte(cxt, i); |
9dea2923 | 874 | |
af0df606 | 875 | if (start == pe->offset) |
cf3808e4 KZ |
876 | start += cxt->first_lba; |
877 | lastplusoff = last[i] + ((part_n < 4) ? 0 : cxt->first_lba); | |
9dea2923 DB |
878 | if (start >= first[i] && start <= lastplusoff) |
879 | start = lastplusoff + 1; | |
880 | } | |
881 | ||
882 | return start; | |
883 | } | |
884 | ||
e3661531 | 885 | static void fill_bounds(struct fdisk_context *cxt, |
0073a4cf | 886 | fdisk_sector_t *first, fdisk_sector_t *last) |
cc0c4e56 | 887 | { |
e3661531 | 888 | size_t i; |
af0df606 | 889 | struct pte *pe = self_pte(cxt, 0); |
f540fe15 | 890 | struct dos_partition *p; |
cc0c4e56 | 891 | |
e3661531 | 892 | for (i = 0; i < cxt->label->nparts_max; pe++,i++) { |
6864f168 | 893 | p = pe->pt_entry; |
d0a870c5 | 894 | if (is_cleared_partition(p) || IS_EXTENDED (p->sys_ind)) { |
cc0c4e56 KZ |
895 | first[i] = 0xffffffff; |
896 | last[i] = 0; | |
897 | } else { | |
61eaad50 | 898 | first[i] = get_abs_partition_start(pe); |
1b92d076 | 899 | last[i] = get_abs_partition_end(pe); |
cc0c4e56 KZ |
900 | } |
901 | } | |
902 | } | |
903 | ||
77d6a70a | 904 | static int get_start_from_user( struct fdisk_context *cxt, |
0073a4cf KZ |
905 | fdisk_sector_t *start, |
906 | fdisk_sector_t low, | |
907 | fdisk_sector_t dflt, | |
908 | fdisk_sector_t limit, | |
77d6a70a | 909 | struct fdisk_partition *pa) |
9dea2923 | 910 | { |
77d6a70a KZ |
911 | assert(start); |
912 | ||
913 | /* try to use tepmlate from 'pa' */ | |
914 | if (pa && pa->start_follow_default) | |
915 | *start = dflt; | |
916 | ||
ecf40cda | 917 | else if (pa && fdisk_partition_has_start(pa)) { |
88141067 | 918 | DBG(LABEL, ul_debug("DOS: start: wanted=%ju, low=%ju, limit=%ju", |
20f878fe | 919 | (uintmax_t) pa->start, (uintmax_t) low, (uintmax_t) limit)); |
77d6a70a | 920 | *start = pa->start; |
20f878fe | 921 | if (*start < low || *start > limit) { |
a3e37fa7 KZ |
922 | fdisk_warnx(cxt, _("Start sector %ju out of range."), |
923 | (uintmax_t) *start); | |
77d6a70a | 924 | return -ERANGE; |
20f878fe | 925 | } |
77d6a70a KZ |
926 | } else { |
927 | /* ask user by dialog */ | |
928 | struct fdisk_ask *ask = fdisk_new_ask(); | |
929 | int rc; | |
930 | ||
931 | if (!ask) | |
932 | return -ENOMEM; | |
933 | fdisk_ask_set_query(ask, | |
6a632136 | 934 | fdisk_use_cylinders(cxt) ? |
77d6a70a KZ |
935 | _("First cylinder") : _("First sector")); |
936 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER); | |
937 | fdisk_ask_number_set_low(ask, fdisk_cround(cxt, low)); | |
938 | fdisk_ask_number_set_default(ask, fdisk_cround(cxt, dflt)); | |
939 | fdisk_ask_number_set_high(ask, fdisk_cround(cxt, limit)); | |
940 | ||
941 | rc = fdisk_do_ask(cxt, ask); | |
942 | *start = fdisk_ask_number_get_result(ask); | |
a3d83488 | 943 | fdisk_unref_ask(ask); |
77d6a70a KZ |
944 | if (rc) |
945 | return rc; | |
6a632136 | 946 | if (fdisk_use_cylinders(cxt)) { |
77d6a70a | 947 | *start = (*start - 1) |
6a632136 | 948 | * fdisk_get_units_per_sector(cxt); |
77d6a70a KZ |
949 | if (*start < low) |
950 | *start = low; | |
951 | } | |
952 | } | |
953 | ||
c3bc7483 | 954 | DBG(LABEL, ul_debug("DOS: start is %ju", (uintmax_t) *start)); |
77d6a70a KZ |
955 | return 0; |
956 | } | |
957 | ||
0073a4cf | 958 | static fdisk_sector_t get_possible_last(struct fdisk_context *cxt, size_t n) |
333c3761 | 959 | { |
0073a4cf | 960 | fdisk_sector_t limit; |
333c3761 KZ |
961 | |
962 | if (n >= 4) { | |
963 | /* logical partitions */ | |
964 | struct fdisk_dos_label *l = self_label(cxt); | |
965 | struct pte *ext_pe = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL; | |
966 | ||
967 | if (!ext_pe) | |
968 | return 0; | |
969 | limit = get_abs_partition_end(ext_pe); | |
970 | } else { | |
971 | /* primary partitions */ | |
972 | if (fdisk_use_cylinders(cxt) || !cxt->total_sectors) | |
973 | limit = cxt->geom.heads * cxt->geom.sectors * cxt->geom.cylinders - 1; | |
974 | else | |
975 | limit = cxt->total_sectors - 1; | |
976 | ||
977 | if (limit > UINT_MAX) | |
978 | limit = UINT_MAX; | |
979 | } | |
980 | ||
981 | DBG(LABEL, ul_debug("DOS: last possible sector for #%zu is %ju", | |
982 | n, (uintmax_t) limit)); | |
983 | return limit; | |
984 | } | |
985 | ||
986 | /* returns last free sector for area addressed by @start, the first[] and | |
987 | * last[] are fill_bounds() results */ | |
0073a4cf KZ |
988 | static fdisk_sector_t get_unused_last(struct fdisk_context *cxt, size_t n, |
989 | fdisk_sector_t start, | |
990 | fdisk_sector_t first[], fdisk_sector_t last[]) | |
333c3761 KZ |
991 | { |
992 | size_t i; | |
0073a4cf | 993 | fdisk_sector_t limit = get_possible_last(cxt, n); |
333c3761 KZ |
994 | |
995 | for (i = 0; i < cxt->label->nparts_max; i++) { | |
996 | struct pte *pe = self_pte(cxt, i); | |
997 | ||
998 | if (start < pe->offset && limit >= pe->offset) | |
999 | limit = pe->offset - 1; | |
1000 | if (start < first[i] && limit >= first[i]) | |
1001 | limit = first[i] - 1; | |
1002 | } | |
1003 | ||
1004 | DBG(LABEL, ul_debug("DOS: unused sector for #%zu is %ju", | |
1005 | n, (uintmax_t) limit)); | |
1006 | return limit; | |
1007 | } | |
1008 | ||
77d6a70a KZ |
1009 | static int add_partition(struct fdisk_context *cxt, size_t n, |
1010 | struct fdisk_partition *pa) | |
1011 | { | |
1012 | int sys, read = 0, rc, isrel = 0; | |
e3661531 | 1013 | size_t i; |
fcea064b | 1014 | struct fdisk_dos_label *l = self_label(cxt); |
af0df606 | 1015 | struct dos_partition *p = self_partition(cxt, n); |
1b92d076 | 1016 | struct pte *ext_pe = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL; |
af0df606 | 1017 | |
0073a4cf | 1018 | fdisk_sector_t start, stop = 0, limit, temp, |
e3661531 KZ |
1019 | first[cxt->label->nparts_max], |
1020 | last[cxt->label->nparts_max]; | |
9dea2923 | 1021 | |
88141067 | 1022 | DBG(LABEL, ul_debug("DOS: adding partition %zu", n)); |
9c825fa0 | 1023 | |
a745611d | 1024 | sys = pa && pa->type ? pa->type->code : MBR_LINUX_DATA_PARTITION; |
ed470672 | 1025 | |
d0a870c5 | 1026 | if (is_used_partition(p)) { |
829f4206 | 1027 | fdisk_warnx(cxt, _("Partition %zu is already defined. " |
e39966c6 | 1028 | "Delete it before re-adding it."), |
829f4206 | 1029 | n + 1); |
8254c3a5 | 1030 | return -EINVAL; |
9dea2923 | 1031 | } |
e3661531 | 1032 | fill_bounds(cxt, first, last); |
333c3761 KZ |
1033 | limit = get_possible_last(cxt, n); |
1034 | ||
9dea2923 | 1035 | if (n < 4) { |
aa36c2cf | 1036 | if (cxt->parent && fdisk_is_label(cxt->parent, GPT)) |
433d05ff | 1037 | start = 1; /* Bad boy modifies hybrid MBR */ |
bcdeacd7 | 1038 | else { |
ecf40cda KZ |
1039 | if (cxt->script && pa && fdisk_partition_has_start(pa) |
1040 | && pa->start < cxt->first_lba | |
bcdeacd7 KZ |
1041 | && pa->start >= 1) |
1042 | fdisk_set_first_lba(cxt, 1); | |
1043 | ||
433d05ff | 1044 | start = cxt->first_lba; |
bcdeacd7 | 1045 | } |
433d05ff | 1046 | |
fcea064b | 1047 | if (l->ext_offset) { |
1b92d076 | 1048 | assert(ext_pe); |
fcea064b | 1049 | first[l->ext_index] = l->ext_offset; |
1b92d076 | 1050 | last[l->ext_index] = get_abs_partition_end(ext_pe); |
9dea2923 DB |
1051 | } |
1052 | } else { | |
1b92d076 | 1053 | assert(ext_pe); |
bcdeacd7 | 1054 | |
ecf40cda KZ |
1055 | if (cxt->script && pa && fdisk_partition_has_start(pa) |
1056 | && pa->start >= l->ext_offset | |
bcdeacd7 KZ |
1057 | && pa->start < l->ext_offset + cxt->first_lba) |
1058 | fdisk_set_first_lba(cxt, 1); | |
1059 | ||
1060 | start = l->ext_offset + cxt->first_lba; | |
9dea2923 | 1061 | } |
333c3761 | 1062 | |
6a632136 | 1063 | if (fdisk_use_cylinders(cxt)) |
80063996 | 1064 | for (i = 0; i < cxt->label->nparts_max; i++) { |
77d6a70a | 1065 | first[i] = (fdisk_cround(cxt, first[i]) - 1) |
6a632136 | 1066 | * fdisk_get_units_per_sector(cxt); |
80063996 | 1067 | } |
9dea2923 | 1068 | |
0e07540d KZ |
1069 | /* |
1070 | * Ask for first sector | |
1071 | */ | |
9dea2923 | 1072 | do { |
0073a4cf | 1073 | fdisk_sector_t dflt, aligned; |
9dea2923 DB |
1074 | |
1075 | temp = start; | |
cf3808e4 | 1076 | dflt = start = get_unused_start(cxt, n, start, first, last); |
9dea2923 | 1077 | |
ecf40cda | 1078 | if (n >= 4 && pa && fdisk_partition_has_start(pa) && cxt->script |
bcdeacd7 KZ |
1079 | && cxt->first_lba > 1 |
1080 | && temp == start - cxt->first_lba) { | |
1081 | fdisk_set_first_lba(cxt, 1); | |
1082 | start = pa->start; | |
1083 | } | |
1084 | ||
9dea2923 DB |
1085 | /* the default sector should be aligned and unused */ |
1086 | do { | |
9475cc78 | 1087 | aligned = fdisk_align_lba_in_range(cxt, dflt, dflt, limit); |
cf3808e4 | 1088 | dflt = get_unused_start(cxt, n, aligned, first, last); |
9dea2923 DB |
1089 | } while (dflt != aligned && dflt > aligned && dflt < limit); |
1090 | ||
1091 | if (dflt >= limit) | |
1092 | dflt = start; | |
1093 | if (start > limit) | |
1094 | break; | |
6a632136 | 1095 | if (start >= temp + fdisk_get_units_per_sector(cxt) |
fcea064b | 1096 | && read) { |
ac1a559a | 1097 | fdisk_info(cxt, _("Sector %llu is already allocated."), |
fcea064b | 1098 | temp); |
9dea2923 DB |
1099 | temp = start; |
1100 | read = 0; | |
ecf40cda KZ |
1101 | if (pa && (fdisk_partition_has_start(pa) || |
1102 | pa->start_follow_default)) | |
3186f4a9 | 1103 | break; |
9dea2923 | 1104 | } |
0e07540d | 1105 | |
9dea2923 | 1106 | if (!read && start == temp) { |
77d6a70a | 1107 | rc = get_start_from_user(cxt, &start, temp, dflt, limit, pa); |
0e07540d KZ |
1108 | if (rc) |
1109 | return rc; | |
9dea2923 DB |
1110 | read = 1; |
1111 | } | |
1112 | } while (start != temp || !read); | |
0e07540d | 1113 | |
613596b3 KZ |
1114 | if (n == 4) { |
1115 | /* The first EBR is stored at begin of the extended partition */ | |
1116 | struct pte *pe = self_pte(cxt, n); | |
1117 | pe->offset = l->ext_offset; | |
1118 | ||
1119 | } else if (n > 4) { | |
1120 | /* The second (and another) EBR */ | |
af0df606 | 1121 | struct pte *pe = self_pte(cxt, n); |
9dea2923 | 1122 | |
cf3808e4 | 1123 | pe->offset = start - cxt->first_lba; |
fcea064b | 1124 | if (pe->offset == l->ext_offset) { /* must be corrected */ |
9dea2923 | 1125 | pe->offset++; |
cf3808e4 | 1126 | if (cxt->first_lba == 1) |
9dea2923 DB |
1127 | start++; |
1128 | } | |
1129 | } | |
1130 | ||
333c3761 | 1131 | limit = get_unused_last(cxt, n, start, first, last); |
9dea2923 | 1132 | |
9dea2923 | 1133 | if (start > limit) { |
ac1a559a | 1134 | fdisk_info(cxt, _("No free sectors available.")); |
9dea2923 | 1135 | if (n > 4) |
e3661531 | 1136 | cxt->label->nparts_max--; |
8254c3a5 | 1137 | return -ENOSPC; |
9dea2923 | 1138 | } |
77d6a70a KZ |
1139 | |
1140 | /* | |
1141 | * Ask for last sector | |
1142 | */ | |
1143 | if (fdisk_cround(cxt, start) == fdisk_cround(cxt, limit)) | |
1144 | stop = limit; | |
1145 | else if (pa && pa->end_follow_default) | |
9dea2923 | 1146 | stop = limit; |
ecf40cda | 1147 | else if (pa && fdisk_partition_has_size(pa)) { |
bcdeacd7 KZ |
1148 | stop = start + pa->size - 1; |
1149 | isrel = pa->size_explicit ? 0 : 1; | |
9dea2923 | 1150 | } else { |
77d6a70a | 1151 | /* ask user by dialog */ |
0e07540d | 1152 | struct fdisk_ask *ask = fdisk_new_ask(); |
9dea2923 | 1153 | |
77d6a70a KZ |
1154 | if (!ask) |
1155 | return -ENOMEM; | |
0e07540d | 1156 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET); |
9dea2923 | 1157 | |
6a632136 | 1158 | if (fdisk_use_cylinders(cxt)) { |
0e07540d KZ |
1159 | fdisk_ask_set_query(ask, _("Last cylinder, +cylinders or +size{K,M,G,T,P}")); |
1160 | fdisk_ask_number_set_unit(ask, | |
1161 | cxt->sector_size * | |
6a632136 | 1162 | fdisk_get_units_per_sector(cxt)); |
0e07540d KZ |
1163 | } else { |
1164 | fdisk_ask_set_query(ask, _("Last sector, +sectors or +size{K,M,G,T,P}")); | |
1165 | fdisk_ask_number_set_unit(ask,cxt->sector_size); | |
1166 | } | |
1167 | ||
77d6a70a KZ |
1168 | fdisk_ask_number_set_low(ask, fdisk_cround(cxt, start)); |
1169 | fdisk_ask_number_set_default(ask, fdisk_cround(cxt, limit)); | |
1170 | fdisk_ask_number_set_high(ask, fdisk_cround(cxt, limit)); | |
1171 | fdisk_ask_number_set_base(ask, fdisk_cround(cxt, start)); /* base for relative input */ | |
0e07540d KZ |
1172 | |
1173 | rc = fdisk_do_ask(cxt, ask); | |
0e07540d | 1174 | stop = fdisk_ask_number_get_result(ask); |
77d6a70a | 1175 | isrel = fdisk_ask_number_is_relative(ask); |
a3d83488 | 1176 | fdisk_unref_ask(ask); |
77d6a70a KZ |
1177 | if (rc) |
1178 | return rc; | |
6a632136 KZ |
1179 | if (fdisk_use_cylinders(cxt)) { |
1180 | stop = stop * fdisk_get_units_per_sector(cxt) - 1; | |
77d6a70a KZ |
1181 | if (stop >limit) |
1182 | stop = limit; | |
a389baf1 | 1183 | } |
77d6a70a KZ |
1184 | } |
1185 | ||
bcdeacd7 KZ |
1186 | DBG(LABEL, ul_debug("DOS: raw stop: %ju", (uintmax_t) stop)); |
1187 | ||
77d6a70a KZ |
1188 | if (stop > limit) |
1189 | stop = limit; | |
1190 | ||
1191 | if (stop < limit) { | |
1192 | if (isrel && alignment_required(cxt)) { | |
9dea2923 | 1193 | /* the last sector has not been exactly requested (but |
0e07540d KZ |
1194 | * defined by +size{K,M,G} convention), so be smart and |
1195 | * align the end of the partition. The next partition | |
1196 | * will start at phy.block boundary. | |
9dea2923 | 1197 | */ |
9475cc78 | 1198 | stop = fdisk_align_lba_in_range(cxt, stop, start, limit) - 1; |
9dea2923 DB |
1199 | if (stop > limit) |
1200 | stop = limit; | |
1201 | } | |
1202 | } | |
1203 | ||
ecf40cda | 1204 | set_partition(cxt, n, 0, start, stop, sys, pa && pa->boot == 1 ? 1 : 0); |
af0df606 KZ |
1205 | if (n > 4) { |
1206 | struct pte *pe = self_pte(cxt, n); | |
bb58112e | 1207 | set_partition(cxt, n - 1, 1, pe->offset, stop, |
7bc31ada | 1208 | MBR_DOS_EXTENDED_PARTITION, 0); |
af0df606 | 1209 | } |
9dea2923 | 1210 | |
0ecf3ab5 KZ |
1211 | /* report */ |
1212 | { | |
1213 | struct fdisk_parttype *t = | |
1214 | fdisk_label_get_parttype_from_code(cxt->label, sys); | |
1215 | fdisk_info_new_partition(cxt, n + 1, start, stop, t); | |
dfc6db2a | 1216 | fdisk_unref_parttype(t); |
0ecf3ab5 KZ |
1217 | } |
1218 | ||
1219 | ||
bb58112e | 1220 | if (IS_EXTENDED(sys)) { |
af0df606 | 1221 | struct pte *pen = self_pte(cxt, n); |
9dea2923 | 1222 | |
fcea064b | 1223 | l->ext_index = n; |
613596b3 | 1224 | l->ext_offset = start; |
6864f168 | 1225 | pen->ex_entry = p; |
9dea2923 | 1226 | } |
8254c3a5 | 1227 | |
bddd84e7 | 1228 | fdisk_label_set_changed(cxt->label, 1); |
8254c3a5 | 1229 | return 0; |
9dea2923 DB |
1230 | } |
1231 | ||
c3bc7483 KZ |
1232 | static int add_logical(struct fdisk_context *cxt, |
1233 | struct fdisk_partition *pa, | |
1234 | size_t *partno) | |
9dea2923 | 1235 | { |
613596b3 | 1236 | struct pte *pe; |
28d79d9b | 1237 | int rc; |
af0df606 | 1238 | |
e3661531 | 1239 | assert(cxt); |
c3bc7483 | 1240 | assert(partno); |
e3661531 | 1241 | assert(cxt->label); |
613596b3 | 1242 | assert(self_label(cxt)->ext_offset); |
e3661531 | 1243 | |
613596b3 KZ |
1244 | DBG(LABEL, ul_debug("DOS: nparts max: %zu", cxt->label->nparts_max)); |
1245 | pe = self_pte(cxt, cxt->label->nparts_max); | |
af0df606 | 1246 | |
613596b3 | 1247 | if (!pe->sectorbuffer) { |
af0df606 KZ |
1248 | pe->sectorbuffer = calloc(1, cxt->sector_size); |
1249 | if (!pe->sectorbuffer) | |
1250 | return -ENOMEM; | |
613596b3 KZ |
1251 | DBG(LABEL, ul_debug("DOS: logical: %zu: new EBR sector buffer %p", |
1252 | cxt->label->nparts_max, pe->sectorbuffer)); | |
ddec0e40 | 1253 | pe->private_sectorbuffer = 1; |
9dea2923 | 1254 | } |
613596b3 KZ |
1255 | pe->pt_entry = mbr_get_partition(pe->sectorbuffer, 0); |
1256 | pe->ex_entry = pe->pt_entry + 1; | |
1257 | pe->offset = 0; | |
1258 | partition_set_changed(cxt, cxt->label->nparts_max, 1); | |
1259 | ||
1260 | cxt->label->nparts_max++; | |
1261 | ||
a536cf71 KZ |
1262 | /* this message makes sense only when we use extended/primary/logical |
1263 | * dialog. The dialog is disable for scripts, see dos_add_partition() */ | |
1264 | if (!cxt->script) | |
1265 | fdisk_info(cxt, _("Adding logical partition %zu"), | |
1266 | cxt->label->nparts_max); | |
c3bc7483 | 1267 | *partno = cxt->label->nparts_max - 1; |
28d79d9b KZ |
1268 | rc = add_partition(cxt, *partno, pa); |
1269 | ||
1270 | if (rc) { | |
1271 | /* reset on error */ | |
1272 | cxt->label->nparts_max--; | |
1273 | pe->pt_entry = NULL; | |
1274 | pe->ex_entry = NULL; | |
1275 | pe->offset = 0; | |
1276 | pe->changed = 0; | |
1277 | } | |
1278 | ||
1279 | return rc; | |
9dea2923 DB |
1280 | } |
1281 | ||
e3661531 | 1282 | static void check(struct fdisk_context *cxt, size_t n, |
5dfca634 KZ |
1283 | unsigned int h, unsigned int s, unsigned int c, |
1284 | unsigned int start) | |
1285 | { | |
1286 | unsigned int total, real_s, real_c; | |
1287 | ||
63fb717d KZ |
1288 | if (!is_dos_compatible(cxt)) |
1289 | return; | |
1290 | ||
5dfca634 KZ |
1291 | real_s = sector(s) - 1; |
1292 | real_c = cylinder(s, c); | |
9bb8caff | 1293 | total = (real_c * cxt->geom.heads + h) * cxt->geom.sectors + real_s; |
fcea064b | 1294 | |
5dfca634 | 1295 | if (!total) |
829f4206 | 1296 | fdisk_warnx(cxt, _("Partition %zu: contains sector 0"), n); |
5dfca634 | 1297 | if (h >= cxt->geom.heads) |
829f4206 | 1298 | fdisk_warnx(cxt, _("Partition %zu: head %d greater than " |
fcea064b | 1299 | "maximum %d"), n, h + 1, cxt->geom.heads); |
5dfca634 | 1300 | if (real_s >= cxt->geom.sectors) |
829f4206 | 1301 | fdisk_warnx(cxt, _("Partition %zu: sector %d greater than " |
fcea064b | 1302 | "maximum %llu"), n, s, cxt->geom.sectors); |
5dfca634 | 1303 | if (real_c >= cxt->geom.cylinders) |
829f4206 | 1304 | fdisk_warnx(cxt, _("Partition %zu: cylinder %d greater than " |
fcea064b KZ |
1305 | "maximum %llu"), |
1306 | n, real_c + 1, | |
1307 | cxt->geom.cylinders); | |
1308 | ||
5dfca634 | 1309 | if (cxt->geom.cylinders <= 1024 && start != total) |
829f4206 KZ |
1310 | fdisk_warnx(cxt, _("Partition %zu: previous sectors %u " |
1311 | "disagrees with total %u"), n, start, total); | |
5dfca634 KZ |
1312 | } |
1313 | ||
1314 | /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993, | |
1315 | * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross, | |
1316 | * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S. | |
1317 | * Lubkin Oct. 1991). */ | |
1318 | ||
1319 | static void | |
1320 | long2chs(struct fdisk_context *cxt, unsigned long ls, | |
1321 | unsigned int *c, unsigned int *h, unsigned int *s) { | |
1322 | int spc = cxt->geom.heads * cxt->geom.sectors; | |
1323 | ||
1324 | *c = ls / spc; | |
1325 | ls = ls % spc; | |
1326 | *h = ls / cxt->geom.sectors; | |
1327 | *s = ls % cxt->geom.sectors + 1; /* sectors count from 1 */ | |
1328 | } | |
1329 | ||
f540fe15 | 1330 | static void check_consistency(struct fdisk_context *cxt, struct dos_partition *p, |
e3661531 | 1331 | size_t partition) |
5dfca634 KZ |
1332 | { |
1333 | unsigned int pbc, pbh, pbs; /* physical beginning c, h, s */ | |
1334 | unsigned int pec, peh, pes; /* physical ending c, h, s */ | |
1335 | unsigned int lbc, lbh, lbs; /* logical beginning c, h, s */ | |
1336 | unsigned int lec, leh, les; /* logical ending c, h, s */ | |
1337 | ||
1338 | if (!is_dos_compatible(cxt)) | |
1339 | return; | |
1340 | ||
1341 | if (!cxt->geom.heads || !cxt->geom.sectors || (partition >= 4)) | |
1342 | return; /* do not check extended partitions */ | |
1343 | ||
60c1b036 | 1344 | /* physical beginning c, h, s */ |
da967236 KZ |
1345 | pbc = (p->bc & 0xff) | ((p->bs << 2) & 0x300); |
1346 | pbh = p->bh; | |
1347 | pbs = p->bs & 0x3f; | |
5dfca634 | 1348 | |
60c1b036 | 1349 | /* physical ending c, h, s */ |
da967236 KZ |
1350 | pec = (p->ec & 0xff) | ((p->es << 2) & 0x300); |
1351 | peh = p->eh; | |
1352 | pes = p->es & 0x3f; | |
5dfca634 | 1353 | |
60c1b036 | 1354 | /* compute logical beginning (c, h, s) */ |
b44244cb | 1355 | long2chs(cxt, dos_partition_get_start(p), &lbc, &lbh, &lbs); |
5dfca634 | 1356 | |
60c1b036 | 1357 | /* compute logical ending (c, h, s) */ |
b44244cb | 1358 | long2chs(cxt, dos_partition_get_start(p) + dos_partition_get_size(p) - 1, &lec, &leh, &les); |
5dfca634 | 1359 | |
60c1b036 | 1360 | /* Same physical / logical beginning? */ |
fcea064b KZ |
1361 | if (cxt->geom.cylinders <= 1024 |
1362 | && (pbc != lbc || pbh != lbh || pbs != lbs)) { | |
829f4206 | 1363 | fdisk_warnx(cxt, _("Partition %zu: different physical/logical " |
fcea064b KZ |
1364 | "beginnings (non-Linux?): " |
1365 | "phys=(%d, %d, %d), logical=(%d, %d, %d)"), | |
1366 | partition + 1, | |
1367 | pbc, pbh, pbs, | |
1368 | lbc, lbh, lbs); | |
5dfca634 KZ |
1369 | } |
1370 | ||
60c1b036 | 1371 | /* Same physical / logical ending? */ |
fcea064b KZ |
1372 | if (cxt->geom.cylinders <= 1024 |
1373 | && (pec != lec || peh != leh || pes != les)) { | |
829f4206 | 1374 | fdisk_warnx(cxt, _("Partition %zu: different physical/logical " |
fcea064b KZ |
1375 | "endings: phys=(%d, %d, %d), logical=(%d, %d, %d)"), |
1376 | partition + 1, | |
1377 | pec, peh, pes, | |
1378 | lec, leh, les); | |
5dfca634 KZ |
1379 | } |
1380 | ||
60c1b036 | 1381 | /* Ending on cylinder boundary? */ |
5dfca634 | 1382 | if (peh != (cxt->geom.heads - 1) || pes != cxt->geom.sectors) { |
829f4206 | 1383 | fdisk_warnx(cxt, _("Partition %zu: does not end on " |
fcea064b | 1384 | "cylinder boundary."), |
5dfca634 KZ |
1385 | partition + 1); |
1386 | } | |
1387 | } | |
1388 | ||
9ffeb235 | 1389 | static int dos_verify_disklabel(struct fdisk_context *cxt) |
2ca61a61 | 1390 | { |
e3661531 | 1391 | size_t i, j; |
0073a4cf KZ |
1392 | fdisk_sector_t total = 1, n_sectors = cxt->total_sectors; |
1393 | fdisk_sector_t first[cxt->label->nparts_max], | |
1394 | last[cxt->label->nparts_max]; | |
f540fe15 | 1395 | struct dos_partition *p; |
fcea064b | 1396 | struct fdisk_dos_label *l = self_label(cxt); |
2ca61a61 | 1397 | |
aa36c2cf | 1398 | assert(fdisk_is_label(cxt, DOS)); |
9ffeb235 | 1399 | |
e3661531 KZ |
1400 | fill_bounds(cxt, first, last); |
1401 | for (i = 0; i < cxt->label->nparts_max; i++) { | |
af0df606 | 1402 | struct pte *pe = self_pte(cxt, i); |
2ca61a61 | 1403 | |
af0df606 | 1404 | p = self_partition(cxt, i); |
d0a870c5 | 1405 | if (is_used_partition(p) && !IS_EXTENDED(p->sys_ind)) { |
2ca61a61 | 1406 | check_consistency(cxt, p, i); |
61eaad50 | 1407 | if (get_abs_partition_start(pe) < first[i]) |
fcea064b | 1408 | fdisk_warnx(cxt, _( |
829f4206 | 1409 | "Partition %zu: bad start-of-data."), |
fcea064b KZ |
1410 | i + 1); |
1411 | ||
1412 | check(cxt, i + 1, p->eh, p->es, p->ec, last[i]); | |
2ca61a61 | 1413 | total += last[i] + 1 - first[i]; |
fcea064b | 1414 | |
11712b66 KZ |
1415 | if (i == 0) |
1416 | total += get_abs_partition_start(pe) - 1; | |
1417 | ||
fcea064b | 1418 | for (j = 0; j < i; j++) { |
2ca61a61 DB |
1419 | if ((first[i] >= first[j] && first[i] <= last[j]) |
1420 | || ((last[i] <= last[j] && last[i] >= first[j]))) { | |
fcea064b | 1421 | |
829f4206 KZ |
1422 | fdisk_warnx(cxt, _("Partition %zu: " |
1423 | "overlaps partition %zu."), | |
fcea064b KZ |
1424 | j + 1, i + 1); |
1425 | ||
2ca61a61 DB |
1426 | total += first[i] >= first[j] ? |
1427 | first[i] : first[j]; | |
1428 | total -= last[i] <= last[j] ? | |
1429 | last[i] : last[j]; | |
1430 | } | |
fcea064b | 1431 | } |
2ca61a61 DB |
1432 | } |
1433 | } | |
1434 | ||
fcea064b | 1435 | if (l->ext_offset) { |
0073a4cf | 1436 | fdisk_sector_t e_last; |
1b92d076 KZ |
1437 | struct pte *ext_pe = self_pte(cxt, l->ext_index); |
1438 | ||
1439 | e_last = get_abs_partition_end(ext_pe); | |
2ca61a61 | 1440 | |
e3661531 | 1441 | for (i = 4; i < cxt->label->nparts_max; i++) { |
2ca61a61 | 1442 | total++; |
af0df606 KZ |
1443 | p = self_partition(cxt, i); |
1444 | ||
2ca61a61 | 1445 | if (!p->sys_ind) { |
e3661531 | 1446 | if (i != 4 || i + 1 < cxt->label->nparts_max) |
fcea064b | 1447 | fdisk_warnx(cxt, |
829f4206 | 1448 | _("Partition %zu: empty."), |
fcea064b KZ |
1449 | i + 1); |
1450 | } else if (first[i] < l->ext_offset | |
1451 | || last[i] > e_last) { | |
1452 | ||
829f4206 KZ |
1453 | fdisk_warnx(cxt, _("Logical partition %zu: " |
1454 | "not entirely in partition %zu."), | |
fcea064b | 1455 | i + 1, l->ext_index + 1); |
2ca61a61 | 1456 | } |
2ca61a61 DB |
1457 | } |
1458 | } | |
1459 | ||
1460 | if (total > n_sectors) | |
fcea064b KZ |
1461 | fdisk_warnx(cxt, _("Total allocated sectors %llu greater " |
1462 | "than the maximum %llu."), total, n_sectors); | |
2ca61a61 | 1463 | else if (total < n_sectors) |
fcea064b | 1464 | fdisk_warnx(cxt, _("Remaining %lld unallocated %ld-byte " |
ac1a559a | 1465 | "sectors."), n_sectors - total, cxt->sector_size); |
2ca61a61 DB |
1466 | |
1467 | return 0; | |
1468 | } | |
1469 | ||
9dea2923 DB |
1470 | /* |
1471 | * Ask the user for new partition type information (logical, extended). | |
0f639e54 DB |
1472 | * This function calls the actual partition adding logic - add_partition. |
1473 | * | |
1474 | * API callback. | |
9dea2923 | 1475 | */ |
77d6a70a | 1476 | static int dos_add_partition(struct fdisk_context *cxt, |
c3bc7483 KZ |
1477 | struct fdisk_partition *pa, |
1478 | size_t *partno) | |
9dea2923 | 1479 | { |
ea987bb5 | 1480 | size_t i, free_primary = 0, free_sectors = 0; |
0073a4cf | 1481 | fdisk_sector_t last = 0, grain; |
e3661531 | 1482 | int rc = 0; |
e77313a6 | 1483 | struct fdisk_dos_label *l; |
1b92d076 | 1484 | struct pte *ext_pe; |
c3bc7483 | 1485 | size_t res; /* partno */ |
9dea2923 | 1486 | |
9ffeb235 KZ |
1487 | assert(cxt); |
1488 | assert(cxt->label); | |
aa36c2cf | 1489 | assert(fdisk_is_label(cxt, DOS)); |
9ffeb235 | 1490 | |
c3bc7483 KZ |
1491 | DBG(LABEL, ul_debug("DOS: new partition wanted")); |
1492 | ||
e77313a6 | 1493 | l = self_label(cxt); |
1b92d076 | 1494 | ext_pe = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL; |
a3e37fa7 | 1495 | |
c3bc7483 KZ |
1496 | /* |
1497 | * partition template (@pa) based partitioning | |
1498 | */ | |
1499 | ||
a3e37fa7 | 1500 | /* pa specifies start within extended partition, add logical */ |
ecf40cda | 1501 | if (pa && fdisk_partition_has_start(pa) && ext_pe |
a3e37fa7 | 1502 | && pa->start >= l->ext_offset |
1b92d076 | 1503 | && pa->start <= get_abs_partition_end(ext_pe)) { |
c3bc7483 KZ |
1504 | DBG(LABEL, ul_debug("DOS: pa template %p: add logical", pa)); |
1505 | rc = add_logical(cxt, pa, &res); | |
a3e37fa7 KZ |
1506 | goto done; |
1507 | ||
ea987bb5 KZ |
1508 | /* pa specifies that extended partition is wanted */ |
1509 | } else if (pa && pa->type && pa->type->code == MBR_DOS_EXTENDED_PARTITION) { | |
c3bc7483 | 1510 | DBG(LABEL, ul_debug("DOS: pa template %p: add extened", pa)); |
ea987bb5 KZ |
1511 | if (l->ext_offset) { |
1512 | fdisk_warnx(cxt, _("Extended partition already exists.")); | |
1513 | return -EINVAL; | |
1514 | } | |
44374b10 BE |
1515 | rc = get_partition_unused_primary(cxt, pa, &res); |
1516 | if (rc == 0) { | |
c3bc7483 KZ |
1517 | rc = add_partition(cxt, res, pa); |
1518 | goto done; | |
1519 | } | |
1520 | ||
1521 | /* pa specifies start, but outside extended partition */ | |
ecf40cda | 1522 | } else if (pa && fdisk_partition_has_start(pa) && l->ext_offset) { |
c3bc7483 | 1523 | DBG(LABEL, ul_debug("DOS: pa template %p: add primary", pa)); |
44374b10 BE |
1524 | rc = get_partition_unused_primary(cxt, pa, &res); |
1525 | if (rc == 0) { | |
c3bc7483 | 1526 | rc = add_partition(cxt, res, pa); |
ea987bb5 KZ |
1527 | goto done; |
1528 | } | |
a3e37fa7 | 1529 | } |
20f878fe | 1530 | |
c3bc7483 KZ |
1531 | /* |
1532 | * dialog driven partitioning (it does not mean that @pa template is | |
1533 | * completely ignored!) | |
1534 | */ | |
1535 | ||
ea987bb5 KZ |
1536 | /* check if there is space for primary partition */ |
1537 | grain = cxt->grain > cxt->sector_size ? cxt->grain / cxt->sector_size : 1; | |
1538 | last = cxt->first_lba; | |
1539 | ||
af0df606 KZ |
1540 | for (i = 0; i < 4; i++) { |
1541 | struct dos_partition *p = self_partition(cxt, i); | |
ea987bb5 KZ |
1542 | |
1543 | if (is_used_partition(p)) { | |
0073a4cf | 1544 | fdisk_sector_t start = dos_partition_get_start(p); |
ea987bb5 KZ |
1545 | if (last + grain <= start) |
1546 | free_sectors = 1; | |
1547 | last = start + dos_partition_get_size(p); | |
1548 | } else | |
1549 | free_primary++; | |
af0df606 | 1550 | } |
ea987bb5 KZ |
1551 | if (last + grain < cxt->total_sectors - 1) |
1552 | free_sectors = 1; | |
9dea2923 | 1553 | |
e3661531 | 1554 | if (!free_primary && cxt->label->nparts_max >= MAXIMUM_PARTS) { |
fcea064b KZ |
1555 | fdisk_info(cxt, _("The maximum number of partitions has " |
1556 | "been created.")); | |
8254c3a5 | 1557 | return -EINVAL; |
9dea2923 | 1558 | } |
1bf3a034 | 1559 | rc = 1; |
fcea064b | 1560 | |
ea987bb5 | 1561 | if (!free_primary || !free_sectors) { |
c3bc7483 | 1562 | DBG(LABEL, ul_debug("DOS: primary impossible, add logical")); |
fcea064b | 1563 | if (l->ext_offset) { |
ecf40cda | 1564 | if (!pa || fdisk_partition_has_start(pa)) { |
c3bc7483 KZ |
1565 | if (!free_primary) |
1566 | fdisk_info(cxt, _("All primary partitions are in use.")); | |
1567 | else if (!free_sectors) | |
1568 | fdisk_info(cxt, _("All space for primary partitions is in use.")); | |
1569 | } | |
1570 | rc = add_logical(cxt, pa, &res); | |
ea987bb5 | 1571 | } else { |
c3bc7483 KZ |
1572 | fdisk_info(cxt, |
1573 | _( "Impossible to create another primary partition. " | |
1574 | "If you want to create more partitions, you must " | |
1575 | "replace a primary partition with an extended " | |
fcea064b | 1576 | "partition first.")); |
ea987bb5 KZ |
1577 | return -EINVAL; |
1578 | } | |
e3661531 | 1579 | } else if (cxt->label->nparts_max >= MAXIMUM_PARTS) { |
fcea064b KZ |
1580 | fdisk_info(cxt, _("All logical partitions are in use. " |
1581 | "Adding a primary partition.")); | |
44374b10 BE |
1582 | rc = get_partition_unused_primary(cxt, pa, &res); |
1583 | if (rc == 0) | |
c3bc7483 | 1584 | rc = add_partition(cxt, res, pa); |
9dea2923 | 1585 | } else { |
20f878fe KZ |
1586 | char hint[BUFSIZ]; |
1587 | struct fdisk_ask *ask; | |
1588 | int c; | |
1589 | ||
ea987bb5 KZ |
1590 | /* the default layout for scripts is to create primary partitions */ |
1591 | if (cxt->script) { | |
44374b10 BE |
1592 | rc = get_partition_unused_primary(cxt, pa, &res); |
1593 | if (rc == 0) | |
c3bc7483 | 1594 | rc = add_partition(cxt, res, pa); |
ea987bb5 KZ |
1595 | goto done; |
1596 | } | |
1597 | ||
20f878fe KZ |
1598 | ask = fdisk_new_ask(); |
1599 | if (!ask) | |
1600 | return -ENOMEM; | |
1601 | fdisk_ask_set_type(ask, FDISK_ASKTYPE_MENU); | |
1602 | fdisk_ask_set_query(ask, _("Partition type")); | |
1603 | fdisk_ask_menu_set_default(ask, free_primary == 1 | |
1604 | && !l->ext_offset ? 'e' : 'p'); | |
1605 | snprintf(hint, sizeof(hint), | |
1606 | _("%zu primary, %d extended, %zu free"), | |
1607 | 4 - (l->ext_offset ? 1 : 0) - free_primary, | |
1608 | l->ext_offset ? 1 : 0, | |
1609 | free_primary); | |
1610 | ||
1611 | fdisk_ask_menu_add_item(ask, 'p', _("primary"), hint); | |
1612 | if (!l->ext_offset) | |
1613 | fdisk_ask_menu_add_item(ask, 'e', _("extended"), _("container for logical partitions")); | |
1614 | else | |
1615 | fdisk_ask_menu_add_item(ask, 'l', _("logical"), _("numbered from 5")); | |
1616 | ||
1617 | rc = fdisk_do_ask(cxt, ask); | |
bd0d850b KZ |
1618 | if (rc) |
1619 | return rc; | |
20f878fe | 1620 | fdisk_ask_menu_get_result(ask, &c); |
a3d83488 | 1621 | fdisk_unref_ask(ask); |
bd0d850b | 1622 | |
9dea2923 | 1623 | if (c == 'p') { |
44374b10 BE |
1624 | rc = get_partition_unused_primary(cxt, pa, &res); |
1625 | if (rc == 0) | |
c3bc7483 | 1626 | rc = add_partition(cxt, res, pa); |
8254c3a5 | 1627 | goto done; |
fcea064b | 1628 | } else if (c == 'l' && l->ext_offset) { |
c3bc7483 | 1629 | rc = add_logical(cxt, pa, &res); |
8254c3a5 | 1630 | goto done; |
fcea064b | 1631 | } else if (c == 'e' && !l->ext_offset) { |
44374b10 BE |
1632 | rc = get_partition_unused_primary(cxt, pa, &res); |
1633 | if (rc == 0) { | |
ecf40cda | 1634 | struct fdisk_partition *xpa = NULL; |
77d6a70a KZ |
1635 | struct fdisk_parttype *t; |
1636 | ||
a745611d | 1637 | t = fdisk_label_get_parttype_from_code(cxt->label, |
bb58112e | 1638 | MBR_DOS_EXTENDED_PARTITION); |
ecf40cda KZ |
1639 | if (!pa) { |
1640 | pa = xpa = fdisk_new_partition(); | |
1641 | if (!xpa) | |
1642 | return -ENOMEM; | |
1643 | } | |
77d6a70a | 1644 | fdisk_partition_set_type(pa, t); |
c3bc7483 | 1645 | rc = add_partition(cxt, res, pa); |
ecf40cda KZ |
1646 | if (xpa) { |
1647 | fdisk_unref_partition(xpa); | |
1648 | pa = NULL; | |
1649 | } | |
ed470672 | 1650 | } |
8254c3a5 | 1651 | goto done; |
9dea2923 | 1652 | } else |
ac1a559a | 1653 | fdisk_warnx(cxt, _("Invalid partition type `%c'."), c); |
9dea2923 | 1654 | } |
8254c3a5 | 1655 | done: |
c3bc7483 | 1656 | if (rc == 0) { |
e3661531 | 1657 | cxt->label->nparts_cur++; |
44374b10 | 1658 | if (partno) |
c3bc7483 KZ |
1659 | *partno = res; |
1660 | } | |
8254c3a5 | 1661 | return rc; |
9dea2923 | 1662 | } |
0dc13a38 | 1663 | |
0073a4cf | 1664 | static int write_sector(struct fdisk_context *cxt, fdisk_sector_t secno, |
fae7b1bc | 1665 | unsigned char *buf) |
0dc13a38 | 1666 | { |
21770662 KZ |
1667 | int rc; |
1668 | ||
1669 | rc = seek_sector(cxt, secno); | |
1670 | if (rc != 0) { | |
98aeae96 | 1671 | fdisk_warn(cxt, _("Cannot write sector %jd: seek failed"), |
21770662 KZ |
1672 | (uintmax_t) secno); |
1673 | return rc; | |
1674 | } | |
bcddbe96 | 1675 | |
88141067 | 1676 | DBG(LABEL, ul_debug("DOS: writting to sector %ju", (uintmax_t) secno)); |
bcddbe96 | 1677 | |
fae7b1bc DB |
1678 | if (write(cxt->dev_fd, buf, cxt->sector_size) != (ssize_t) cxt->sector_size) |
1679 | return -errno; | |
1680 | return 0; | |
1681 | } | |
1682 | ||
9ffeb235 | 1683 | static int dos_write_disklabel(struct fdisk_context *cxt) |
fae7b1bc | 1684 | { |
8b3d9241 | 1685 | struct fdisk_dos_label *l = self_label(cxt); |
e3661531 | 1686 | size_t i; |
8b3d9241 | 1687 | int rc = 0, mbr_changed = 0; |
0dc13a38 | 1688 | |
9ffeb235 KZ |
1689 | assert(cxt); |
1690 | assert(cxt->label); | |
aa36c2cf | 1691 | assert(fdisk_is_label(cxt, DOS)); |
9ffeb235 | 1692 | |
8b3d9241 KZ |
1693 | mbr_changed = l->non_pt_changed; |
1694 | ||
0dc13a38 | 1695 | /* MBR (primary partitions) */ |
8b3d9241 | 1696 | if (!mbr_changed) { |
af0df606 KZ |
1697 | for (i = 0; i < 4; i++) { |
1698 | struct pte *pe = self_pte(cxt, i); | |
1699 | if (pe->changed) | |
8b3d9241 | 1700 | mbr_changed = 1; |
af0df606 | 1701 | } |
0dc13a38 | 1702 | } |
8b3d9241 | 1703 | if (mbr_changed) { |
67987b47 | 1704 | mbr_set_magic(cxt->firstsector); |
fae7b1bc DB |
1705 | rc = write_sector(cxt, 0, cxt->firstsector); |
1706 | if (rc) | |
1707 | goto done; | |
0dc13a38 | 1708 | } |
8b3d9241 | 1709 | |
bcddbe96 KZ |
1710 | if (cxt->label->nparts_max <= 4 && l->ext_offset) { |
1711 | /* we have empty extended partition, check if the partition has | |
1712 | * been modified and then cleanup possible remaining EBR */ | |
1713 | struct pte *pe = self_pte(cxt, l->ext_index); | |
1714 | unsigned char empty[512] = { 0 }; | |
0073a4cf | 1715 | fdisk_sector_t off = pe ? get_abs_partition_start(pe) : 0; |
bcddbe96 KZ |
1716 | |
1717 | if (off && pe->changed) { | |
1718 | mbr_set_magic(empty); | |
1719 | write_sector(cxt, off, empty); | |
1720 | } | |
1721 | } | |
1722 | ||
0dc13a38 | 1723 | /* EBR (logical partitions) */ |
e3661531 | 1724 | for (i = 4; i < cxt->label->nparts_max; i++) { |
af0df606 | 1725 | struct pte *pe = self_pte(cxt, i); |
0dc13a38 | 1726 | |
28d79d9b | 1727 | if (!pe->changed || !pe->offset || !pe->sectorbuffer) |
613596b3 KZ |
1728 | continue; |
1729 | ||
613596b3 KZ |
1730 | mbr_set_magic(pe->sectorbuffer); |
1731 | rc = write_sector(cxt, pe->offset, pe->sectorbuffer); | |
1732 | if (rc) | |
1733 | goto done; | |
0dc13a38 | 1734 | } |
fae7b1bc DB |
1735 | |
1736 | done: | |
1737 | return rc; | |
0dc13a38 | 1738 | } |
b8855c86 | 1739 | |
f32a9be4 KZ |
1740 | static int dos_locate_disklabel(struct fdisk_context *cxt, int n, |
1741 | const char **name, off_t *offset, size_t *size) | |
1742 | { | |
1743 | assert(cxt); | |
1744 | ||
1745 | *name = NULL; | |
1746 | *offset = 0; | |
1747 | *size = 0; | |
1748 | ||
1749 | switch (n) { | |
1750 | case 0: | |
1751 | *name = "MBR"; | |
1752 | *offset = 0; | |
1753 | *size = 512; | |
1754 | break; | |
1755 | default: | |
1756 | /* extended partitions */ | |
1757 | if (n - 1 + 4 < cxt->label->nparts_max) { | |
1758 | struct pte *pe = self_pte(cxt, n - 1 + 4); | |
1759 | ||
1760 | assert(pe->private_sectorbuffer); | |
1761 | ||
1762 | *name = "EBR"; | |
1763 | *offset = pe->offset * cxt->sector_size; | |
1764 | *size = 512; | |
1765 | } else | |
1766 | return 1; | |
1767 | break; | |
1768 | } | |
1769 | ||
1770 | return 0; | |
1771 | } | |
1772 | ||
dfc96cbf KZ |
1773 | /* |
1774 | * Check whether partition entries are ordered by their starting positions. | |
1775 | * Return 0 if OK. Return i if partition i should have been earlier. | |
1776 | * Two separate checks: primary and logical partitions. | |
1777 | */ | |
e3661531 | 1778 | static int wrong_p_order(struct fdisk_context *cxt, size_t *prev) |
dfc96cbf | 1779 | { |
e3661531 KZ |
1780 | size_t last_p_start_pos = 0, p_start_pos; |
1781 | size_t i, last_i = 0; | |
dfc96cbf | 1782 | |
e3661531 | 1783 | for (i = 0 ; i < cxt->label->nparts_max; i++) { |
af0df606 KZ |
1784 | |
1785 | struct pte *pe = self_pte(cxt, i); | |
1786 | struct dos_partition *p = pe->pt_entry; | |
1787 | ||
dfc96cbf KZ |
1788 | if (i == 4) { |
1789 | last_i = 4; | |
1790 | last_p_start_pos = 0; | |
1791 | } | |
d0a870c5 | 1792 | if (is_used_partition(p)) { |
61eaad50 | 1793 | p_start_pos = get_abs_partition_start(pe); |
dfc96cbf KZ |
1794 | |
1795 | if (last_p_start_pos > p_start_pos) { | |
1796 | if (prev) | |
1797 | *prev = last_i; | |
1798 | return i; | |
1799 | } | |
1800 | ||
1801 | last_p_start_pos = p_start_pos; | |
1802 | last_i = i; | |
1803 | } | |
1804 | } | |
1805 | return 0; | |
1806 | } | |
6d864a49 | 1807 | |
1cf67f1c KZ |
1808 | static int dos_list_disklabel(struct fdisk_context *cxt) |
1809 | { | |
6d864a49 | 1810 | assert(cxt); |
9ffeb235 | 1811 | assert(cxt->label); |
aa36c2cf | 1812 | assert(fdisk_is_label(cxt, DOS)); |
6d864a49 | 1813 | |
6c89f750 | 1814 | return 0; |
262002ee | 1815 | } |
1cf67f1c | 1816 | |
262002ee KZ |
1817 | static int dos_get_partition(struct fdisk_context *cxt, size_t n, |
1818 | struct fdisk_partition *pa) | |
1819 | { | |
1820 | struct dos_partition *p; | |
1821 | struct pte *pe; | |
bd5e8291 | 1822 | struct fdisk_dos_label *lb; |
1cf67f1c | 1823 | |
262002ee KZ |
1824 | assert(cxt); |
1825 | assert(pa); | |
1826 | assert(cxt->label); | |
aa36c2cf | 1827 | assert(fdisk_is_label(cxt, DOS)); |
1cf67f1c | 1828 | |
bd5e8291 | 1829 | lb = self_label(cxt); |
262002ee KZ |
1830 | pe = self_pte(cxt, n); |
1831 | p = pe->pt_entry; | |
1832 | pa->used = !is_cleared_partition(p); | |
1833 | if (!pa->used) | |
1834 | return 0; | |
1cf67f1c | 1835 | |
7f01ec7a | 1836 | pa->type = dos_partition_parttype(cxt, p); |
cb4d6804 | 1837 | pa->boot = p->boot_ind == ACTIVE_FLAG ? 1 : 0; |
77d6a70a | 1838 | pa->start = get_abs_partition_start(pe); |
1b92d076 KZ |
1839 | pa->end = get_abs_partition_end(pe); |
1840 | pa->size = dos_partition_get_size(p); | |
e77313a6 | 1841 | pa->container = lb->ext_offset && n == lb->ext_index; |
1cf67f1c | 1842 | |
bd5e8291 KZ |
1843 | if (n >= 4) |
1844 | pa->parent_partno = lb->ext_index; | |
03643931 | 1845 | |
cb4d6804 | 1846 | if (p->boot_ind && asprintf(&pa->attrs, "%02x", p->boot_ind) < 0) |
262002ee | 1847 | return -ENOMEM; |
6d864a49 | 1848 | |
262002ee | 1849 | /* start C/H/S */ |
ecf40cda | 1850 | if (asprintf(&pa->start_chs, "%d/%d/%d", |
262002ee KZ |
1851 | cylinder(p->bs, p->bc), |
1852 | sector(p->bs), | |
1853 | p->bh) < 0) | |
1854 | return -ENOMEM; | |
1cf67f1c | 1855 | |
262002ee | 1856 | /* end C/H/S */ |
ecf40cda | 1857 | if (asprintf(&pa->end_chs, "%d/%d/%d", |
262002ee KZ |
1858 | cylinder(p->es, p->ec), |
1859 | sector(p->es), | |
1860 | p->eh) < 0) | |
1861 | return -ENOMEM; | |
6d864a49 | 1862 | |
262002ee | 1863 | return 0; |
6d864a49 KZ |
1864 | } |
1865 | ||
0ecf3ab5 KZ |
1866 | static int dos_set_partition(struct fdisk_context *cxt, size_t n, |
1867 | struct fdisk_partition *pa) | |
1868 | { | |
333c3761 | 1869 | struct fdisk_dos_label *l; |
0ecf3ab5 KZ |
1870 | struct dos_partition *p; |
1871 | struct pte *pe; | |
0073a4cf | 1872 | fdisk_sector_t start, size; |
0ecf3ab5 KZ |
1873 | |
1874 | assert(cxt); | |
1875 | assert(pa); | |
1876 | assert(cxt->label); | |
1877 | assert(fdisk_is_label(cxt, DOS)); | |
1878 | ||
1879 | if (n >= cxt->label->nparts_max) | |
1880 | return -EINVAL; | |
1881 | ||
1882 | if (pa->type && IS_EXTENDED(pa->type->code)) { | |
1883 | fdisk_warnx(cxt, _("You cannot change a partition into an " | |
1884 | "extended one or vice versa. Delete it first.")); | |
1885 | return -EINVAL; | |
1886 | } | |
1887 | ||
1888 | if (pa->type && !pa->type->code) | |
1889 | fdisk_warnx(cxt, _("Type 0 means free space to many systems. " | |
1890 | "Having partitions of type 0 is probably unwise.")); | |
333c3761 KZ |
1891 | l = self_label(cxt); |
1892 | p = self_partition(cxt, n); | |
1893 | pe = self_pte(cxt, n); | |
150d98ee | 1894 | |
333c3761 KZ |
1895 | FDISK_INIT_UNDEF(start); |
1896 | FDISK_INIT_UNDEF(size); | |
0ecf3ab5 | 1897 | |
333c3761 KZ |
1898 | if (fdisk_partition_has_start(pa)) |
1899 | start = pa->start; | |
1900 | if (fdisk_partition_has_size(pa)) | |
1901 | size = pa->size; | |
150d98ee | 1902 | |
333c3761 | 1903 | if (pa->end_follow_default) { |
0073a4cf | 1904 | fdisk_sector_t first[cxt->label->nparts_max], |
333c3761 KZ |
1905 | last[cxt->label->nparts_max], |
1906 | xlast; | |
1907 | struct pte *ext = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL; | |
1908 | ||
1909 | fill_bounds(cxt, first, last); | |
1910 | ||
1911 | if (ext && l->ext_offset) { | |
1912 | first[l->ext_index] = l->ext_offset; | |
1913 | last[l->ext_index] = get_abs_partition_end(ext); | |
1914 | } | |
1915 | if (FDISK_IS_UNDEF(start)) | |
1916 | start = get_abs_partition_start(pe); | |
1917 | ||
1918 | DBG(LABEL, ul_debug("DOS: #%zu now %ju +%ju sectors", | |
1919 | n, (uintmax_t) start, (uintmax_t) dos_partition_get_size(p))); | |
1920 | ||
1921 | xlast = get_unused_last(cxt, n, start, first, last); | |
1922 | size = xlast ? xlast - start + 1: dos_partition_get_size(p); | |
ecf40cda | 1923 | |
333c3761 KZ |
1924 | DBG(LABEL, ul_debug("DOS: #%zu wanted %ju +%ju sectors", |
1925 | n, (uintmax_t) start, (uintmax_t) size)); | |
1926 | } | |
1927 | ||
1928 | if (!FDISK_IS_UNDEF(start) || !FDISK_IS_UNDEF(size)) { | |
1929 | DBG(LABEL, ul_debug("DOS: resize partition")); | |
150d98ee | 1930 | |
333c3761 KZ |
1931 | if (FDISK_IS_UNDEF(start)) |
1932 | start = get_abs_partition_start(pe); | |
1933 | if (FDISK_IS_UNDEF(size)) | |
1934 | size = dos_partition_get_size(p); | |
0ecf3ab5 | 1935 | |
150d98ee | 1936 | set_partition(cxt, n, 0, start, start + size - 1, |
0ecf3ab5 | 1937 | pa->type ? pa->type->code : p->sys_ind, |
ecf40cda | 1938 | pa->boot == 1); |
150d98ee | 1939 | } else { |
ecf40cda | 1940 | DBG(LABEL, ul_debug("DOS: keep size, modify properties")); |
150d98ee KZ |
1941 | if (pa->type) |
1942 | p->sys_ind = pa->type->code; | |
ecf40cda | 1943 | if (!FDISK_IS_UNDEF(pa->boot)) |
150d98ee KZ |
1944 | p->boot_ind = pa->boot == 1 ? ACTIVE_FLAG : 0; |
1945 | } | |
1946 | ||
0ecf3ab5 KZ |
1947 | partition_set_changed(cxt, n, 1); |
1948 | return 0; | |
1949 | } | |
1950 | ||
a60087aa KZ |
1951 | static void print_chain_of_logicals(struct fdisk_context *cxt) |
1952 | { | |
1953 | size_t i; | |
6c64adee | 1954 | struct fdisk_dos_label *l = self_label(cxt); |
a60087aa KZ |
1955 | |
1956 | fputc('\n', stdout); | |
1957 | ||
1958 | for (i = 4; i < cxt->label->nparts_max; i++) { | |
1959 | struct pte *pe = self_pte(cxt, i); | |
1960 | ||
2248aaf9 | 1961 | fprintf(stderr, "#%02zu EBR [%10ju], " |
6c64adee KZ |
1962 | "data[start=%10ju (%10ju), size=%10ju], " |
1963 | "link[start=%10ju (%10ju), size=%10ju]\n", | |
a60087aa | 1964 | i, (uintmax_t) pe->offset, |
6c64adee | 1965 | /* data */ |
a60087aa | 1966 | (uintmax_t) dos_partition_get_start(pe->pt_entry), |
6c64adee | 1967 | (uintmax_t) get_abs_partition_start(pe), |
a60087aa | 1968 | (uintmax_t) dos_partition_get_size(pe->pt_entry), |
6c64adee | 1969 | /* link */ |
a60087aa | 1970 | (uintmax_t) dos_partition_get_start(pe->ex_entry), |
6c64adee | 1971 | (uintmax_t) l->ext_offset + dos_partition_get_start(pe->ex_entry), |
a60087aa KZ |
1972 | (uintmax_t) dos_partition_get_size(pe->ex_entry)); |
1973 | } | |
1974 | } | |
1975 | ||
c1156d85 KZ |
1976 | static int cmp_ebr_offsets(const void *a, const void *b) |
1977 | { | |
1978 | struct pte *ae = (struct pte *) a, | |
1979 | *be = (struct pte *) b; | |
1980 | ||
99cacc6a | 1981 | if (ae->offset == 0 && be->offset == 0) |
c1156d85 KZ |
1982 | return 0; |
1983 | if (ae->offset == 0) | |
1984 | return 1; | |
1985 | if (be->offset == 0) | |
1986 | return -1; | |
1987 | ||
19ff8ff7 | 1988 | return cmp_numbers(ae->offset, be->offset); |
c1156d85 KZ |
1989 | } |
1990 | ||
dfc96cbf KZ |
1991 | /* |
1992 | * Fix the chain of logicals. | |
dfc96cbf | 1993 | * |
a60087aa KZ |
1994 | * The function does not modify data partitions within EBR tables |
1995 | * (pte->pt_entry). It sorts the chain by EBR offsets and then update links | |
1996 | * (pte->ex_entry) between EBR tables. | |
1997 | * | |
dfc96cbf | 1998 | */ |
e3661531 | 1999 | static void fix_chain_of_logicals(struct fdisk_context *cxt) |
dfc96cbf | 2000 | { |
af0df606 | 2001 | struct fdisk_dos_label *l = self_label(cxt); |
d0bdf184 | 2002 | struct pte *last; |
a60087aa KZ |
2003 | size_t i; |
2004 | ||
a60087aa KZ |
2005 | DBG(LABEL, print_chain_of_logicals(cxt)); |
2006 | ||
c1156d85 KZ |
2007 | /* Sort chain by EBR offsets */ |
2008 | qsort(&l->ptes[4], cxt->label->nparts_max - 4, sizeof(struct pte), | |
2009 | cmp_ebr_offsets); | |
2010 | ||
a60087aa | 2011 | again: |
c1156d85 | 2012 | /* Sort data partitions by start */ |
a60087aa KZ |
2013 | for (i = 4; i < cxt->label->nparts_max - 1; i++) { |
2014 | struct pte *cur = self_pte(cxt, i), | |
2015 | *nxt = self_pte(cxt, i + 1); | |
a60087aa | 2016 | |
c1156d85 KZ |
2017 | if (get_abs_partition_start(cur) > |
2018 | get_abs_partition_start(nxt)) { | |
a60087aa | 2019 | |
c1156d85 | 2020 | struct dos_partition tmp = *cur->pt_entry; |
0073a4cf | 2021 | fdisk_sector_t cur_start = get_abs_partition_start(cur), |
c1156d85 | 2022 | nxt_start = get_abs_partition_start(nxt); |
a60087aa | 2023 | |
c1156d85 KZ |
2024 | /* swap data partitions */ |
2025 | *cur->pt_entry = *nxt->pt_entry; | |
2026 | *nxt->pt_entry = tmp; | |
a60087aa | 2027 | |
c1156d85 KZ |
2028 | /* Recount starts according to EBR offsets, the absolute |
2029 | * address tas to be still the same! */ | |
2030 | dos_partition_set_start(cur->pt_entry, nxt_start - cur->offset); | |
2031 | dos_partition_set_start(nxt->pt_entry, cur_start - nxt->offset); | |
a60087aa | 2032 | |
c1156d85 KZ |
2033 | partition_set_changed(cxt, i, 1); |
2034 | partition_set_changed(cxt, i + 1, 1); | |
2035 | goto again; | |
2036 | } | |
dfc96cbf KZ |
2037 | } |
2038 | ||
c1156d85 | 2039 | /* Update EBR links */ |
a60087aa KZ |
2040 | for (i = 4; i < cxt->label->nparts_max - 1; i++) { |
2041 | struct pte *cur = self_pte(cxt, i), | |
2042 | *nxt = self_pte(cxt, i + 1); | |
2043 | ||
0073a4cf | 2044 | fdisk_sector_t noff = nxt->offset - l->ext_offset, |
a60087aa KZ |
2045 | ooff = dos_partition_get_start(cur->ex_entry); |
2046 | ||
a60087aa KZ |
2047 | if (noff == ooff) |
2048 | continue; | |
2049 | ||
2050 | DBG(LABEL, ul_debug("DOS: fix EBR [%10ju] link %ju -> %ju", | |
2051 | (uintmax_t) cur->offset, | |
2052 | (uintmax_t) ooff, (uintmax_t) noff)); | |
2053 | ||
2054 | set_partition(cxt, i, 1, nxt->offset, | |
7bc31ada KZ |
2055 | get_abs_partition_end(nxt), |
2056 | MBR_DOS_EXTENDED_PARTITION, 0); | |
d0bdf184 | 2057 | } |
6c64adee | 2058 | |
d0bdf184 KZ |
2059 | /* always terminate the chain ! */ |
2060 | last = self_pte(cxt, cxt->label->nparts_max - 1); | |
2061 | if (last) { | |
2062 | clear_partition(last->ex_entry); | |
2063 | partition_set_changed(cxt, cxt->label->nparts_max - 1, 1); | |
a60087aa | 2064 | } |
d0bdf184 | 2065 | |
a60087aa | 2066 | DBG(LABEL, print_chain_of_logicals(cxt)); |
dfc96cbf KZ |
2067 | } |
2068 | ||
dd7ba604 | 2069 | static int dos_reorder(struct fdisk_context *cxt) |
dfc96cbf KZ |
2070 | { |
2071 | struct pte *pei, *pek; | |
e3661531 | 2072 | size_t i,k; |
dfc96cbf | 2073 | |
e3661531 | 2074 | if (!wrong_p_order(cxt, NULL)) { |
fcea064b | 2075 | fdisk_info(cxt, _("Nothing to do. Ordering is correct already.")); |
f8ad3899 | 2076 | return 0; |
dfc96cbf KZ |
2077 | } |
2078 | ||
e3661531 | 2079 | while ((i = wrong_p_order(cxt, &k)) != 0 && i < 4) { |
dfc96cbf KZ |
2080 | /* partition i should have come earlier, move it */ |
2081 | /* We have to move data in the MBR */ | |
f540fe15 | 2082 | struct dos_partition *pi, *pk, *pe, pbuf; |
af0df606 KZ |
2083 | pei = self_pte(cxt, i); |
2084 | pek = self_pte(cxt, k); | |
dfc96cbf | 2085 | |
6864f168 KZ |
2086 | pe = pei->ex_entry; |
2087 | pei->ex_entry = pek->ex_entry; | |
2088 | pek->ex_entry = pe; | |
dfc96cbf | 2089 | |
6864f168 KZ |
2090 | pi = pei->pt_entry; |
2091 | pk = pek->pt_entry; | |
dfc96cbf | 2092 | |
f540fe15 KZ |
2093 | memmove(&pbuf, pi, sizeof(struct dos_partition)); |
2094 | memmove(pi, pk, sizeof(struct dos_partition)); | |
2095 | memmove(pk, &pbuf, sizeof(struct dos_partition)); | |
dfc96cbf | 2096 | |
af0df606 KZ |
2097 | partition_set_changed(cxt, i, 1); |
2098 | partition_set_changed(cxt, k, 1); | |
dfc96cbf KZ |
2099 | } |
2100 | ||
2101 | if (i) | |
e3661531 | 2102 | fix_chain_of_logicals(cxt); |
dfc96cbf | 2103 | |
f61d2632 | 2104 | fdisk_info(cxt, _("Done.")); |
f8ad3899 | 2105 | return 0; |
dfc96cbf | 2106 | } |
6d864a49 | 2107 | |
ecf40cda | 2108 | /* TODO: use fdisk_set_partition() API */ |
829f4206 | 2109 | int fdisk_dos_move_begin(struct fdisk_context *cxt, size_t i) |
71ee484e | 2110 | { |
f8ad3899 KZ |
2111 | struct pte *pe; |
2112 | struct dos_partition *p; | |
71ee484e | 2113 | unsigned int new, free_start, curr_start, last; |
bd0d850b | 2114 | uintmax_t res = 0; |
e3661531 | 2115 | size_t x; |
f8ad3899 | 2116 | int rc; |
71ee484e KZ |
2117 | |
2118 | assert(cxt); | |
aa36c2cf | 2119 | assert(fdisk_is_label(cxt, DOS)); |
71ee484e | 2120 | |
f8ad3899 KZ |
2121 | pe = self_pte(cxt, i); |
2122 | p = pe->pt_entry; | |
2123 | ||
d0a870c5 | 2124 | if (!is_used_partition(p) || IS_EXTENDED (p->sys_ind)) { |
829f4206 | 2125 | fdisk_warnx(cxt, _("Partition %zu: no data area."), i + 1); |
f8ad3899 | 2126 | return 0; |
71ee484e KZ |
2127 | } |
2128 | ||
2129 | /* the default start is at the second sector of the disk or at the | |
2130 | * second sector of the extended partition | |
2131 | */ | |
2132 | free_start = pe->offset ? pe->offset + 1 : 1; | |
2133 | ||
61eaad50 | 2134 | curr_start = get_abs_partition_start(pe); |
71ee484e KZ |
2135 | |
2136 | /* look for a free space before the current start of the partition */ | |
e3661531 | 2137 | for (x = 0; x < cxt->label->nparts_max; x++) { |
71ee484e | 2138 | unsigned int end; |
af0df606 | 2139 | struct pte *prev_pe = self_pte(cxt, x); |
f540fe15 | 2140 | struct dos_partition *prev_p = prev_pe->pt_entry; |
71ee484e KZ |
2141 | |
2142 | if (!prev_p) | |
2143 | continue; | |
af0df606 KZ |
2144 | end = get_abs_partition_start(prev_pe) |
2145 | + dos_partition_get_size(prev_p); | |
71ee484e | 2146 | |
d0a870c5 | 2147 | if (is_used_partition(prev_p) && |
71ee484e KZ |
2148 | end > free_start && end <= curr_start) |
2149 | free_start = end; | |
2150 | } | |
2151 | ||
1b92d076 | 2152 | last = get_abs_partition_end(pe); |
71ee484e | 2153 | |
f8ad3899 KZ |
2154 | rc = fdisk_ask_number(cxt, free_start, curr_start, last, |
2155 | _("New beginning of data"), &res); | |
2156 | if (rc) | |
2157 | return rc; | |
2158 | ||
bd0d850b | 2159 | new = res - pe->offset; |
71ee484e | 2160 | |
b44244cb | 2161 | if (new != dos_partition_get_size(p)) { |
af0df606 KZ |
2162 | unsigned int sects = dos_partition_get_size(p) |
2163 | + dos_partition_get_start(p) - new; | |
2164 | ||
b44244cb KZ |
2165 | dos_partition_set_size(p, sects); |
2166 | dos_partition_set_start(p, new); | |
af0df606 KZ |
2167 | |
2168 | partition_set_changed(cxt, i, 1); | |
71ee484e | 2169 | } |
f8ad3899 KZ |
2170 | |
2171 | return rc; | |
71ee484e | 2172 | } |
6d864a49 | 2173 | |
8c0a7f91 | 2174 | static int dos_partition_is_used( |
47b8e7c0 | 2175 | struct fdisk_context *cxt, |
8c0a7f91 | 2176 | size_t i) |
47b8e7c0 | 2177 | { |
f540fe15 | 2178 | struct dos_partition *p; |
47b8e7c0 KZ |
2179 | |
2180 | assert(cxt); | |
9ffeb235 | 2181 | assert(cxt->label); |
aa36c2cf | 2182 | assert(fdisk_is_label(cxt, DOS)); |
47b8e7c0 | 2183 | |
8c0a7f91 KZ |
2184 | if (i >= cxt->label->nparts_max) |
2185 | return 0; | |
47b8e7c0 | 2186 | |
af0df606 | 2187 | p = self_partition(cxt, i); |
47b8e7c0 | 2188 | |
8c0a7f91 | 2189 | return p && !is_cleared_partition(p); |
47b8e7c0 KZ |
2190 | } |
2191 | ||
fb1caca7 KZ |
2192 | static int dos_toggle_partition_flag( |
2193 | struct fdisk_context *cxt, | |
2194 | size_t i, | |
2195 | unsigned long flag) | |
2196 | { | |
f540fe15 | 2197 | struct dos_partition *p; |
fb1caca7 KZ |
2198 | |
2199 | assert(cxt); | |
2200 | assert(cxt->label); | |
aa36c2cf | 2201 | assert(fdisk_is_label(cxt, DOS)); |
fb1caca7 KZ |
2202 | |
2203 | if (i >= cxt->label->nparts_max) | |
2204 | return -EINVAL; | |
2205 | ||
af0df606 | 2206 | p = self_partition(cxt, i); |
fb1caca7 KZ |
2207 | |
2208 | switch (flag) { | |
2209 | case DOS_FLAG_ACTIVE: | |
2210 | if (IS_EXTENDED(p->sys_ind) && !p->boot_ind) | |
829f4206 KZ |
2211 | fdisk_warnx(cxt, _("Partition %zu: is an extended " |
2212 | "partition."), i + 1); | |
fb1caca7 KZ |
2213 | |
2214 | p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG); | |
af0df606 | 2215 | partition_set_changed(cxt, i, 1); |
0477369a | 2216 | fdisk_info(cxt, p->boot_ind ? |
f736ae51 KZ |
2217 | _("The bootable flag on partition %zu is enabled now.") : |
2218 | _("The bootable flag on partition %zu is disabled now."), | |
2219 | i + 1); | |
fb1caca7 KZ |
2220 | break; |
2221 | default: | |
2222 | return 1; | |
2223 | } | |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
bd85d11f | 2228 | static const struct fdisk_field dos_fields[] = |
262002ee KZ |
2229 | { |
2230 | /* basic */ | |
bd85d11f KZ |
2231 | { FDISK_FIELD_DEVICE, N_("Device"), 10, 0 }, |
2232 | { FDISK_FIELD_BOOT, N_("Boot"), 1, 0 }, | |
2233 | { FDISK_FIELD_START, N_("Start"), 5, FDISK_FIELDFL_NUMBER }, | |
2234 | { FDISK_FIELD_END, N_("End"), 5, FDISK_FIELDFL_NUMBER }, | |
2235 | { FDISK_FIELD_SECTORS, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER }, | |
2236 | { FDISK_FIELD_CYLINDERS,N_("Cylinders"), 5, FDISK_FIELDFL_NUMBER }, | |
2237 | { FDISK_FIELD_SIZE, N_("Size"), 5, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_EYECANDY }, | |
2238 | { FDISK_FIELD_TYPEID, N_("Id"), 2, FDISK_FIELDFL_NUMBER }, | |
2239 | { FDISK_FIELD_TYPE, N_("Type"), 0.1, 0 }, | |
262002ee KZ |
2240 | |
2241 | /* expert mode */ | |
bd85d11f KZ |
2242 | { FDISK_FIELD_SADDR, N_("Start-C/H/S"), 1, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL }, |
2243 | { FDISK_FIELD_EADDR, N_("End-C/H/S"), 1, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL }, | |
2244 | { FDISK_FIELD_ATTR, N_("Attrs"), 2, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL } | |
262002ee KZ |
2245 | |
2246 | }; | |
2247 | ||
0c5d095e | 2248 | static const struct fdisk_label_operations dos_operations = |
b8855c86 | 2249 | { |
0c5d095e KZ |
2250 | .probe = dos_probe_label, |
2251 | .write = dos_write_disklabel, | |
2252 | .verify = dos_verify_disklabel, | |
2253 | .create = dos_create_disklabel, | |
f32a9be4 | 2254 | .locate = dos_locate_disklabel, |
1cf67f1c | 2255 | .list = dos_list_disklabel, |
dd7ba604 | 2256 | .reorder = dos_reorder, |
21fe3dde | 2257 | .get_id = dos_get_disklabel_id, |
35b1f0a4 | 2258 | .set_id = dos_set_disklabel_id, |
1cf67f1c | 2259 | |
262002ee | 2260 | .get_part = dos_get_partition, |
0ecf3ab5 | 2261 | .set_part = dos_set_partition, |
77d6a70a | 2262 | .add_part = dos_add_partition, |
e11c6684 | 2263 | .del_part = dos_delete_partition, |
262002ee | 2264 | |
fb1caca7 | 2265 | .part_toggle_flag = dos_toggle_partition_flag, |
8c0a7f91 | 2266 | .part_is_used = dos_partition_is_used, |
47b8e7c0 | 2267 | |
cf3808e4 | 2268 | .reset_alignment = dos_reset_alignment, |
ddec0e40 KZ |
2269 | |
2270 | .deinit = dos_deinit, | |
b8855c86 | 2271 | }; |
0c5d095e KZ |
2272 | |
2273 | /* | |
2274 | * allocates DOS in-memory stuff | |
2275 | */ | |
2276 | struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) | |
2277 | { | |
2278 | struct fdisk_label *lb; | |
2279 | struct fdisk_dos_label *dos; | |
2280 | ||
2281 | assert(cxt); | |
2282 | ||
2283 | dos = calloc(1, sizeof(*dos)); | |
2284 | if (!dos) | |
2285 | return NULL; | |
2286 | ||
2287 | /* initialize generic part of the driver */ | |
2288 | lb = (struct fdisk_label *) dos; | |
2289 | lb->name = "dos"; | |
53b422ab | 2290 | lb->id = FDISK_DISKLABEL_DOS; |
0c5d095e KZ |
2291 | lb->op = &dos_operations; |
2292 | lb->parttypes = dos_parttypes; | |
a745611d | 2293 | lb->nparttypes = ARRAY_SIZE(dos_parttypes) - 1; |
bd85d11f KZ |
2294 | lb->fields = dos_fields; |
2295 | lb->nfields = ARRAY_SIZE(dos_fields); | |
0c5d095e KZ |
2296 | |
2297 | return lb; | |
2298 | } | |
852ce62b | 2299 | |
b7d101a2 KZ |
2300 | /** |
2301 | * fdisk_dos_enable_compatible: | |
2302 | * @lb: DOS label (see fdisk_get_label()) | |
2303 | * @enable: 0 or 1 | |
2304 | * | |
2305 | * Enables deprecated DOS compatible mode, in this mode library checks for | |
2306 | * cylinders boundary, cases about CHS addressing and another obscure things. | |
2307 | * | |
2308 | * Returns: 0 on success, <0 on error. | |
852ce62b | 2309 | */ |
852ce62b KZ |
2310 | int fdisk_dos_enable_compatible(struct fdisk_label *lb, int enable) |
2311 | { | |
2312 | struct fdisk_dos_label *dos = (struct fdisk_dos_label *) lb; | |
2313 | ||
2314 | if (!lb) | |
2315 | return -EINVAL; | |
2316 | ||
2317 | dos->compatible = enable; | |
0b52b94c KZ |
2318 | if (enable) |
2319 | lb->flags |= FDISK_LABEL_FL_REQUIRE_GEOMETRY; | |
852ce62b KZ |
2320 | return 0; |
2321 | } | |
2322 | ||
b7d101a2 KZ |
2323 | /** |
2324 | * fdisk_dos_is_compatible: | |
2325 | * @lb: DOS label | |
2326 | * | |
2327 | * Returns: 0 if DOS compatibility disabled, 1 if enabled | |
2328 | */ | |
852ce62b KZ |
2329 | int fdisk_dos_is_compatible(struct fdisk_label *lb) |
2330 | { | |
2331 | return ((struct fdisk_dos_label *) lb)->compatible; | |
2332 | } |