Debian Codegraph @

libfdisk/src/dos.c

raw
1 /*
2 *
3 * Copyright (C) 2007-2013 Karel Zak <[email protected]>
4 * 2012 Davidlohr Bueso <[email protected]>
5 * 2021 Pali Rohár <[email protected]>
6 *
7 * This is re-written version for libfdisk, the original was fdiskdoslabel.c
8 * from util-linux fdisk.
9 */
10 #include "c.h"
11 #include "randutils.h"
12 #include "pt-mbr.h"
13 #include "strutils.h"
14 #include "sysfs.h"
15
16 #include "fdiskP.h"
17
18 #include <ctype.h>
19
20 #define MAXIMUM_PARTS 60
21 #define ACTIVE_FLAG 0x80
22
23 /**
24 * SECTION: dos
25 * @title: DOS
26 * @short_description: disk label specific functions
27 *
28 */
29
30
31 #define IS_EXTENDED(i) \
32 ((i) == MBR_DOS_EXTENDED_PARTITION \
33 || (i) == MBR_W95_EXTENDED_PARTITION \
34 || (i) == MBR_LINUX_EXTENDED_PARTITION)
35
36 /*
37 * per partition table entry data
38 *
39 * The four primary partitions have the same sectorbuffer
40 * and have NULL ex_entry.
41 *
42 * Each logical partition table entry has two pointers, one for the
43 * partition and one link to the next one.
44 */
45 struct pte {
46 struct dos_partition *pt_entry; /* on-disk MBR entry */
47 struct dos_partition *ex_entry; /* on-disk EBR entry */
48 fdisk_sector_t offset; /* disk sector number */
49 unsigned char *sectorbuffer; /* disk sector contents */
50
51 unsigned int changed : 1,
52 private_sectorbuffer : 1;
53 };
54
55 /*
56 * in-memory fdisk GPT stuff
57 */
58 struct fdisk_dos_label {
59 struct fdisk_label head; /* generic part */
60
61 struct pte ptes[MAXIMUM_PARTS]; /* partition */
62 fdisk_sector_t ext_offset; /* start of the ext.partition */
63 size_t ext_index; /* ext.partition index (if ext_offset is set) */
64 unsigned int compatible : 1, /* is DOS compatible? */
65 non_pt_changed : 1; /* MBR, but no PT changed */
66 };
67
68 /*
69 * Partition types
70 */
71 static const struct fdisk_parttype dos_parttypes[] = {
72 #include "pt-mbr-partnames.h"
73 };
74
75 static const struct fdisk_shortcut dos_parttype_cuts[] =
76 {
77 { .shortcut = "L", .alias = "linux", .data = "83" },
78 { .shortcut = "S", .alias = "swap", .data = "82" },
79 { .shortcut = "E", .alias = "extended", .data = "05", .deprecated = 1 }, /* collision with 0x0e type */
80 { .shortcut = "Ex",.alias = "extended", .data = "05" }, /* MBR extended */
81 { .shortcut = "U", .alias = "uefi", .data = "EF" }, /* UEFI system */
82 { .shortcut = "R", .alias = "raid", .data = "FD" }, /* Linux RAID */
83 { .shortcut = "V", .alias = "lvm", .data = "8E" }, /* LVM */
84 { .shortcut = "X", .alias = "linuxex", .data = "85" } /* Linux extended */
85 };
86
87
88 #define sector(s) ((s) & 0x3f)
89 #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
90
91 #define alignment_required(_x) ((_x)->grain != (_x)->sector_size)
92
93 #define is_dos_compatible(_x) \
94 (fdisk_is_label(_x, DOS) && \
95 fdisk_dos_is_compatible(fdisk_get_label(_x, NULL)))
96
97 #define cround(c, n) fdisk_cround(c, n)
98
99
100 static inline struct fdisk_dos_label *self_label(struct fdisk_context *cxt)
101 {
102 assert(cxt);
103 assert(cxt->label);
104 assert(fdisk_is_label(cxt, DOS));
105
106 return (struct fdisk_dos_label *) cxt->label;
107 }
108
109 static inline struct pte *self_pte(struct fdisk_context *cxt, size_t i)
110 {
111 struct fdisk_dos_label *l = self_label(cxt);
112
113 if (i >= ARRAY_SIZE(l->ptes))
114 return NULL;
115
116 return &l->ptes[i];
117 }
118
119 static inline struct dos_partition *self_partition(
120 struct fdisk_context *cxt,
121 size_t i)
122 {
123 struct pte *pe = self_pte(cxt, i);
124 return pe ? pe->pt_entry : NULL;
125 }
126
127 struct dos_partition *fdisk_dos_get_partition(
128 struct fdisk_context *cxt,
129 size_t i)
130 {
131 assert(cxt);
132 assert(cxt->label);
133 assert(fdisk_is_label(cxt, DOS));
134
135 return self_partition(cxt, i);
136 }
137
138 static struct fdisk_parttype *dos_partition_parttype(
139 struct fdisk_context *cxt,
140 struct dos_partition *p)
141 {
142 struct fdisk_parttype *t
143 = fdisk_label_get_parttype_from_code(cxt->label, p->sys_ind);
144 return t ? : fdisk_new_unknown_parttype(p->sys_ind, NULL);
145 }
146
147 /*
148 * Linux kernel cares about partition size only. Things like
149 * partition type or so are completely irrelevant -- kzak Nov-2013
150 */
151 static int is_used_partition(struct dos_partition *p)
152 {
153 return p && dos_partition_get_size(p) != 0;
154 }
155
156 static void partition_set_changed(
157 struct fdisk_context *cxt,
158 size_t i,
159 int changed)
160 {
161 struct pte *pe = self_pte(cxt, i);
162
163 if (!pe)
164 return;
165
166 DBG(LABEL, ul_debug("DOS: setting %zu partition to %s", i,
167 changed ? "changed" : "unchanged"));
168
169 pe->changed = changed ? 1 : 0;
170 if (changed)
171 fdisk_label_set_changed(cxt->label, 1);
172 }
173
174 static fdisk_sector_t get_abs_partition_start(struct pte *pe)
175 {
176 assert(pe);
177 assert(pe->pt_entry);
178
179 return pe->offset + dos_partition_get_start(pe->pt_entry);
180 }
181
182 static fdisk_sector_t get_abs_partition_end(struct pte *pe)
183 {
184 fdisk_sector_t size;
185
186 assert(pe);
187 assert(pe->pt_entry);
188
189 size = dos_partition_get_size(pe->pt_entry);
190 return get_abs_partition_start(pe) + size - (size ? 1 : 0);
191 }
192
193 static int is_cleared_partition(struct dos_partition *p)
194 {
195 return !(!p || p->boot_ind || p->bh || p->bs || p->bc ||
196 p->sys_ind || p->eh || p->es || p->ec ||
197 dos_partition_get_start(p) || dos_partition_get_size(p));
198 }
199
200 static int get_partition_unused_primary(struct fdisk_context *cxt,
201 struct fdisk_partition *pa,
202 size_t *partno)
203 {
204 size_t org, n;
205 int rc;
206
207 assert(cxt);
208 assert(cxt->label);
209 assert(partno);
210
211 org = cxt->label->nparts_max;
212
213 cxt->label->nparts_max = 4;
214 rc = fdisk_partition_next_partno(pa, cxt, &n);
215 cxt->label->nparts_max = org;
216
217 if (rc == 1) {
218 fdisk_info(cxt, _("All primary partitions have been defined already."));
219 rc = -1;
220 } else if (rc == -ERANGE) {
221 fdisk_warnx(cxt, _("Primary partition not available."));
222 } else if (rc == 0)
223 *partno = n;
224
225 return rc;
226 }
227
228 static int seek_sector(struct fdisk_context *cxt, fdisk_sector_t secno)
229 {
230 off_t offset = (off_t) secno * cxt->sector_size;
231
232 return lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1 ? -errno : 0;
233 }
234
235 static int read_sector(struct fdisk_context *cxt, fdisk_sector_t secno,
236 unsigned char *buf)
237 {
238 int rc = seek_sector(cxt, secno);
239 ssize_t r;
240
241 if (rc < 0)
242 return rc;
243
244 r = read(cxt->dev_fd, buf, cxt->sector_size);
245 if (r == (ssize_t) cxt->sector_size)
246 return 0;
247 if (r < 0)
248 return -errno;
249 return -1;
250 }
251
252 /* Allocate a buffer and read a partition table sector */
253 static int read_pte(struct fdisk_context *cxt, size_t pno, fdisk_sector_t offset)
254 {
255 int rc;
256 unsigned char *buf;
257 struct pte *pe = self_pte(cxt, pno);
258
259 if (!pe)
260 return -EINVAL;
261
262 buf = calloc(1, cxt->sector_size);
263 if (!buf)
264 return -ENOMEM;
265
266 DBG(LABEL, ul_debug("DOS: reading EBR %zu: offset=%ju, buffer=%p",
267 pno, (uintmax_t) offset, buf));
268
269 pe->offset = offset;
270 pe->sectorbuffer = buf;
271 pe->private_sectorbuffer = 1;
272
273 rc = read_sector(cxt, offset, pe->sectorbuffer);
274 if (rc) {
275 fdisk_warn(cxt, _("Failed to read extended partition table "
276 "(offset=%ju)"), (uintmax_t) offset);
277 return rc;
278 }
279
280 pe->changed = 0;
281 pe->pt_entry = pe->ex_entry = NULL;
282 return 0;
283 }
284
285
286 static void clear_partition(struct dos_partition *p)
287 {
288 if (!p)
289 return;
290 p->boot_ind = 0;
291 p->bh = 0;
292 p->bs = 0;
293 p->bc = 0;
294 p->sys_ind = 0;
295 p->eh = 0;
296 p->es = 0;
297 p->ec = 0;
298 dos_partition_set_start(p,0);
299 dos_partition_set_size(p,0);
300 }
301
302 static void dos_init(struct fdisk_context *cxt)
303 {
304 struct fdisk_dos_label *l = self_label(cxt);
305 size_t i;
306
307 assert(cxt);
308 assert(cxt->label);
309 assert(fdisk_is_label(cxt, DOS));
310
311 DBG(LABEL, ul_debug("DOS: initialize, first sector buffer %p", cxt->firstsector));
312
313 cxt->label->nparts_max = 4; /* default, unlimited number of logical */
314
315 l->ext_index = 0;
316 l->ext_offset = 0;
317 l->non_pt_changed = 0;
318
319 memset(l->ptes, 0, sizeof(l->ptes));
320
321 for (i = 0; i < 4; i++) {
322 struct pte *pe = self_pte(cxt, i);
323
324 assert(pe);
325 pe->pt_entry = mbr_get_partition(cxt->firstsector, i);
326 pe->ex_entry = NULL;
327 pe->offset = 0;
328 pe->sectorbuffer = cxt->firstsector;
329 pe->private_sectorbuffer = 0;
330 pe->changed = 0;
331
332 DBG(LABEL, ul_debug("DOS: initialize: #%zu start=%u size=%u sysid=%02x",
333 i,
334 dos_partition_get_start(pe->pt_entry),
335 dos_partition_get_size(pe->pt_entry),
336 pe->pt_entry->sys_ind));
337 }
338
339 if (fdisk_is_listonly(cxt))
340 return;
341 /*
342 * Various warnings...
343 */
344 if (fdisk_missing_geometry(cxt))
345 fdisk_warnx(cxt, _("You can set geometry from the extra functions menu."));
346
347 if (is_dos_compatible(cxt)) {
348 fdisk_warnx(cxt, _("DOS-compatible mode is deprecated."));
349
350 if (cxt->sector_size != cxt->phy_sector_size)
351 fdisk_info(cxt, _(
352 "The device presents a logical sector size that is smaller than "
353 "the physical sector size. Aligning to a physical sector (or optimal "
354 "I/O) size boundary is recommended, or performance may be impacted."));
355 }
356
357 if (fdisk_use_cylinders(cxt))
358 fdisk_warnx(cxt, _("Cylinders as display units are deprecated."));
359
360 if (cxt->total_sectors > UINT_MAX) {
361 uint64_t bytes = cxt->total_sectors * cxt->sector_size;
362 char *szstr = size_to_human_string(SIZE_SUFFIX_SPACE
363 | SIZE_SUFFIX_3LETTER, bytes);
364 fdisk_warnx(cxt,
365 _("The size of this disk is %s (%ju bytes). DOS "
366 "partition table format cannot be used on drives for "
367 "volumes larger than %lu bytes for %lu-byte "
368 "sectors. Use GUID partition table format (GPT)."),
369 szstr, bytes,
370 UINT_MAX * cxt->sector_size,
371 cxt->sector_size);
372 free(szstr);
373 }
374 }
375
376 /* callback called by libfdisk */
377 static void dos_deinit(struct fdisk_label *lb)
378 {
379 size_t i;
380 struct fdisk_dos_label *l = (struct fdisk_dos_label *) lb;
381
382 for (i = 0; i < ARRAY_SIZE(l->ptes); i++) {
383 struct pte *pe = &l->ptes[i];
384
385 if (pe->private_sectorbuffer && pe->sectorbuffer) {
386 DBG(LABEL, ul_debug("DOS: freeing pte %zu sector buffer %p",
387 i, pe->sectorbuffer));
388 free(pe->sectorbuffer);
389 }
390 pe->sectorbuffer = NULL;
391 pe->private_sectorbuffer = 0;
392 }
393
394 memset(l->ptes, 0, sizeof(l->ptes));
395 }
396
397 static void reset_pte(struct pte *pe)
398 {
399 assert(pe);
400
401 if (pe->private_sectorbuffer) {
402 DBG(LABEL, ul_debug(" --> freeing pte sector buffer %p",
403 pe->sectorbuffer));
404 free(pe->sectorbuffer);
405 }
406 memset(pe, 0, sizeof(struct pte));
407 }
408
409 static int delete_partition(struct fdisk_context *cxt, size_t partnum)
410 {
411 struct fdisk_dos_label *l;
412 struct pte *pe;
413 struct dos_partition *p;
414 struct dos_partition *q;
415
416 assert(cxt);
417 assert(cxt->label);
418 assert(fdisk_is_label(cxt, DOS));
419
420 pe = self_pte(cxt, partnum);
421 if (!pe)
422 return -EINVAL;
423
424 DBG(LABEL, ul_debug("DOS: delete partition %zu (max=%zu)", partnum,
425 cxt->label->nparts_max));
426
427 l = self_label(cxt);
428 p = pe->pt_entry;
429 q = pe->ex_entry;
430
431 /* Note that for the fifth partition (partnum == 4) we don't actually
432 decrement partitions. */
433 if (partnum < 4) {
434 DBG(LABEL, ul_debug("--> delete primary"));
435 if (IS_EXTENDED(p->sys_ind) && partnum == l->ext_index) {
436 size_t i;
437 DBG(LABEL, ul_debug(" --> delete extended"));
438 for (i = 4; i < cxt->label->nparts_max; i++) {
439 DBG(LABEL, ul_debug(" --> delete logical #%zu", i));
440 reset_pte(&l->ptes[i]);
441
442 }
443 cxt->label->nparts_max = 4;
444 l->ptes[l->ext_index].ex_entry = NULL;
445 l->ext_offset = 0;
446 l->ext_index = 0;
447 }
448 partition_set_changed(cxt, partnum, 1);
449 clear_partition(p);
450 } else if (!q->sys_ind && partnum > 4) {
451 DBG(LABEL, ul_debug("--> delete logical [last in the chain]"));
452 reset_pte(&l->ptes[partnum]);
453 --cxt->label->nparts_max;
454 --partnum;
455 /* clear link to deleted partition */
456 clear_partition(l->ptes[partnum].ex_entry);
457 partition_set_changed(cxt, partnum, 1);
458 } else {
459 DBG(LABEL, ul_debug("--> delete logical [move down]"));
460 if (partnum > 4) {
461 DBG(LABEL, ul_debug(" --> delete %zu logical link", partnum));
462 p = l->ptes[partnum - 1].ex_entry;
463 *p = *q;
464 dos_partition_set_start(p, dos_partition_get_start(q));
465 dos_partition_set_size(p, dos_partition_get_size(q));
466 dos_partition_sync_chs(p, pe->offset, cxt->geom.sectors, cxt->geom.heads);
467 partition_set_changed(cxt, partnum - 1, 1);
468
469 } else if (cxt->label->nparts_max > 5) {
470 DBG(LABEL, ul_debug(" --> delete first logical link"));
471 pe = &l->ptes[5]; /* second logical */
472
473 if (pe->pt_entry) /* prevent SEGFAULT */
474 dos_partition_set_start(pe->pt_entry,
475 get_abs_partition_start(pe) -
476 l->ext_offset);
477 pe->offset = l->ext_offset;
478 dos_partition_sync_chs(p, pe->offset, cxt->geom.sectors, cxt->geom.heads);
479 partition_set_changed(cxt, 5, 1);
480 }
481
482 if (cxt->label->nparts_max > 5) {
483 DBG(LABEL, ul_debug(" --> move ptes"));
484 cxt->label->nparts_max--;
485 reset_pte(&l->ptes[partnum]);
486 while (partnum < cxt->label->nparts_max) {
487 DBG(LABEL, ul_debug(" --> moving pte %zu <-- %zu", partnum, partnum + 1));
488 l->ptes[partnum] = l->ptes[partnum + 1];
489 partnum++;
490 }
491 memset(&l->ptes[partnum], 0, sizeof(struct pte));
492 } else {
493 DBG(LABEL, ul_debug(" --> the only logical: clear only"));
494 clear_partition(l->ptes[partnum].pt_entry);
495 cxt->label->nparts_max--;
496
497 if (partnum == 4) {
498 DBG(LABEL, ul_debug(" --> clear last logical"));
499 reset_pte(&l->ptes[partnum]);
500 partition_set_changed(cxt, l->ext_index, 1);
501 }
502 }
503 }
504
505 fdisk_label_set_changed(cxt->label, 1);
506 return 0;
507 }
508
509 static int dos_delete_partition(struct fdisk_context *cxt, size_t partnum)
510 {
511 struct pte *pe;
512
513 assert(cxt);
514 assert(cxt->label);
515 assert(fdisk_is_label(cxt, DOS));
516
517 pe = self_pte(cxt, partnum);
518 if (!pe || !is_used_partition(pe->pt_entry))
519 return -EINVAL;
520
521 return delete_partition(cxt, partnum);
522 }
523
524 static void read_extended(struct fdisk_context *cxt, size_t ext)
525 {
526 size_t i;
527 struct pte *pex, *pe;
528 struct dos_partition *p, *q;
529 struct fdisk_dos_label *l = self_label(cxt);
530 fdisk_sector_t ext_start, ext_end;
531
532 if (fdisk_is_listonly(cxt) &&
533 !sysfs_devno_is_wholedisk(fdisk_get_devno(cxt))) {
534 DBG(LABEL, ul_debug("DOS: unable to gather logical partition chain "
535 "when running on a non-whole disk device."));
536 return;
537 }
538
539 l->ext_index = ext;
540 pex = self_pte(cxt, ext);
541 if (!pex) {
542 DBG(LABEL, ul_debug("DOS: uninitialized pointer to %zu pex", ext));
543 return;
544 }
545 pex->ex_entry = pex->pt_entry;
546
547 p = pex->pt_entry;
548 if (!dos_partition_get_start(p)) {
549 fdisk_warnx(cxt, _("Bad offset in primary extended partition."));
550 return;
551 }
552
553 ext_start = get_abs_partition_start(pex);
554 ext_end = get_abs_partition_end(pex);
555
556 DBG(LABEL, ul_debug("DOS: Reading extended %zu", ext));
557
558 while (IS_EXTENDED (p->sys_ind)) {
559 fdisk_sector_t next;
560
561 if (cxt->label->nparts_max >= MAXIMUM_PARTS) {
562 /* This is not a Linux restriction, but
563 this program uses arrays of size MAXIMUM_PARTS.
564 Do not try to `improve' this test. */
565 struct pte *pre = self_pte(cxt,
566 cxt->label->nparts_max - 1);
567 fdisk_warnx(cxt,
568 _("Omitting partitions after #%zu. They will be deleted "
569 "if you save this partition table."),
570 cxt->label->nparts_max);
571
572 if (pre) {
573 clear_partition(pre->ex_entry);
574 partition_set_changed(cxt,
575 cxt->label->nparts_max - 1, 1);
576 }
577 return;
578 }
579
580 pe = self_pte(cxt, cxt->label->nparts_max);
581 if (!pe)
582 return;
583
584 next = l->ext_offset + dos_partition_get_start(p);
585 if (next < ext_start || next > ext_end) {
586 DBG(LABEL, ul_debug("DOS: EBR link %ju outside "
587 "extended partition <%ju-%ju> -- stop",
588 (uintmax_t) next,
589 (uintmax_t) ext_start,
590 (uintmax_t) ext_end));
591 break;
592 }
593
594 if (read_pte(cxt, cxt->label->nparts_max, next))
595 return;
596
597 if (!l->ext_offset)
598 l->ext_offset = dos_partition_get_start(p);
599
600 assert(pe->sectorbuffer);
601 q = p = mbr_get_partition(pe->sectorbuffer, 0);
602
603 for (i = 0; i < 4; i++, p++) {
604 if (!dos_partition_get_size(p))
605 continue;
606
607 if (IS_EXTENDED (p->sys_ind)) {
608 if (pe->ex_entry)
609 fdisk_warnx(cxt, _(
610 "Extra link pointer in partition "
611 "table %zu."),
612 cxt->label->nparts_max + 1);
613 else
614 pe->ex_entry = p;
615 } else if (p->sys_ind) {
616 if (pe->pt_entry)
617 fdisk_warnx(cxt, _(
618 "Ignoring extra data in partition "
619 "table %zu."),
620 cxt->label->nparts_max + 1);
621 else
622 pe->pt_entry = p;
623 }
624 }
625
626 /* very strange code here... */
627 if (!pe->pt_entry) {
628 if (q != pe->ex_entry)
629 pe->pt_entry = q;
630 else
631 pe->pt_entry = q + 1;
632 }
633 if (!pe->ex_entry) {
634 if (q != pe->pt_entry)
635 pe->ex_entry = q;
636 else
637 pe->ex_entry = q + 1;
638 }
639
640 p = pe->ex_entry;
641 cxt->label->nparts_cur = ++cxt->label->nparts_max;
642
643 DBG(LABEL, ul_debug("DOS: EBR[offset=%ju]: link: type=%x, start=%u, size=%u; "
644 " data: type=%x, start=%u, size=%u",
645 (uintmax_t) pe->offset,
646 pe->ex_entry->sys_ind,
647 dos_partition_get_start(pe->ex_entry),
648 dos_partition_get_size(pe->ex_entry),
649 pe->pt_entry->sys_ind,
650 dos_partition_get_start(pe->pt_entry),
651 dos_partition_get_size(pe->pt_entry)));
652
653 }
654
655 /* remove last empty EBR */
656 pe = self_pte(cxt, cxt->label->nparts_max - 1);
657 if (pe &&
658 is_cleared_partition(pe->ex_entry) &&
659 is_cleared_partition(pe->pt_entry)) {
660 DBG(LABEL, ul_debug("DOS: EBR[offset=%ju]: empty, remove", (uintmax_t) pe->offset));
661 reset_pte(pe);
662 cxt->label->nparts_max--;
663 cxt->label->nparts_cur--;
664 }
665
666 /* remove empty links */
667 remove:
668 q = self_partition(cxt, 4);
669 for (i = 4; i < cxt->label->nparts_max; i++) {
670 p = self_partition(cxt, i);
671
672 if (p && !dos_partition_get_size(p) &&
673 (cxt->label->nparts_max > 5 || (q && q->sys_ind))) {
674 fdisk_info(cxt, _("omitting empty partition (%zu)"), i+1);
675 delete_partition(cxt, i);
676 goto remove; /* numbering changed */
677 }
678 }
679
680 DBG(LABEL, ul_debug("DOS: nparts_max: %zu", cxt->label->nparts_max));
681 }
682
683 static int dos_create_disklabel(struct fdisk_context *cxt)
684 {
685 unsigned int id = 0;
686 int rc, has_id = 0;
687 struct fdisk_dos_label *l;
688
689 assert(cxt);
690 assert(cxt->label);
691 assert(fdisk_is_label(cxt, DOS));
692
693 DBG(LABEL, ul_debug("DOS: creating new disklabel"));
694
695 if (cxt->script) {
696 char *end = NULL;
697 const char *s = fdisk_script_get_header(cxt->script, "label-id");
698
699 if (s) {
700 errno = 0;
701 id = strtoul(s, &end, 16);
702 if (!errno && end && s < end) {
703 has_id = 1;
704 DBG(LABEL, ul_debug("DOS: re-use ID from script (0x%08x)", id));
705 } else
706 DBG(LABEL, ul_debug("DOS: failed to parse label=id '%s'", s));
707 }
708 }
709
710 /* random disk signature */
711 if (!has_id) {
712 DBG(LABEL, ul_debug("DOS: generate new ID"));
713 ul_random_get_bytes(&id, sizeof(id));
714 }
715
716 if (fdisk_has_protected_bootbits(cxt))
717 rc = fdisk_init_firstsector_buffer(cxt, 0, MBR_PT_BOOTBITS_SIZE);
718 else
719 rc = fdisk_init_firstsector_buffer(cxt, 0, 0);
720 if (rc)
721 return rc;
722 dos_init(cxt);
723
724 l = self_label(cxt);
725
726 /* Generate an MBR ID for this disk */
727 mbr_set_id(cxt->firstsector, id);
728 l->non_pt_changed = 1;
729 fdisk_label_set_changed(cxt->label, 1);
730
731 /* Put MBR signature */
732 mbr_set_magic(cxt->firstsector);
733
734 fdisk_info(cxt, _("Created a new DOS (MBR) disklabel with disk "
735 "identifier 0x%08x."), id);
736 return 0;
737 }
738
739 static int dos_set_disklabel_id(struct fdisk_context *cxt, const char *str)
740 {
741 char *buf = NULL;
742 unsigned int id, old;
743 struct fdisk_dos_label *l;
744 int rc = 0;
745
746 assert(cxt);
747 assert(cxt->label);
748 assert(fdisk_is_label(cxt, DOS));
749
750 DBG(LABEL, ul_debug("DOS: setting Id"));
751
752 l = self_label(cxt);
753 old = mbr_get_id(cxt->firstsector);
754
755 if (!str) {
756 rc = fdisk_ask_string(cxt,
757 _("Enter the new disk identifier"), &buf);
758 str = buf;
759 }
760 if (!rc) {
761 char *end = NULL;
762
763 errno = 0;
764 id = strtoul(str, &end, 0);
765 if (errno || str == end || (end && *end)) {
766 fdisk_warnx(cxt, _("Incorrect value."));
767 rc = -EINVAL;
768 }
769 }
770
771 free(buf);
772 if (rc)
773 return -EINVAL;
774
775 mbr_set_id(cxt->firstsector, id);
776 l->non_pt_changed = 1;
777 fdisk_label_set_changed(cxt->label, 1);
778
779 fdisk_info(cxt, _("Disk identifier changed from 0x%08x to 0x%08x."),
780 old, id);
781 return 0;
782 }
783
784 static unsigned int chs_div_minus(unsigned int a1, unsigned int a2, unsigned int b1, unsigned int b2)
785 {
786 if (a1 > a2 && b1 > b2) {
787 a1 = a1 - a2;
788 b1 = b1 - b2;
789 } else if (a2 > a1 && b2 > b1) {
790 a1 = a2 - a1;
791 b1 = b2 - b1;
792 } else {
793 return 0;
794 }
795 if (a1 % b1)
796 return 0;
797 return a1 / b1;
798 }
799
800 static inline int chs_overflowed(unsigned int c, unsigned int h, unsigned int s)
801 {
802 /* 1023/254/63 or 1023/255/63 indicates overflowed/invalid C/H/S values */
803 return (c == 1023 && (h == 254 || h == 255) && s == 63);
804 }
805
806 static inline int lba_overflowed(fdisk_sector_t start, fdisk_sector_t sects)
807 {
808 /* Check if the last LBA sector can be represented by unsigned 32bit int */
809 return (start + (sects-1) > UINT32_MAX);
810 }
811
812 static void get_partition_table_geometry(struct fdisk_context *cxt,
813 unsigned int *ph, unsigned int *ps)
814 {
815 unsigned char *bufp = cxt->firstsector;
816 struct { unsigned int c, h, o, v; } t[8] = { 0 };
817 unsigned int n1, n2, n3, n4, n5, n6;
818 const struct dos_partition *p;
819 unsigned int c, h, s, l;
820 unsigned int hh, ss;
821 unsigned int sects;
822 int i, j, dif;
823
824 #define chs_set_t(c, h, s, l, t, i) do { \
825 t[i].c = c; \
826 t[i].h = h; \
827 t[i].o = l - (s-1); \
828 t[i].v = (!chs_overflowed(c, h, s) && s && s-1 <= l); \
829 } while (0)
830
831 /*
832 * Conversion from C/H/S to LBA is defined by formula:
833 * LBA = (c * N_heads + h) * N_sectors + (s - 1)
834 * Let o to be:
835 * o = LBA - (s - 1)
836 * Then formula can be expressed as:
837 * o = (c * N_heads + h) * N_sectors
838 * In general from two tuples (LBA1, c1, h1, s1), (LBA2, c2, h2, s2)
839 * we can derive formulas for N_heads and N_sectors:
840 * N_heads = (o1 * h2 - o2 * h1) / (o2 * c1 - o1 * c2)
841 * N_sectors = (o2 * c1 - o1 * c2) / (c1 * h2 - c2 * h1)
842 * MBR table contains for very partition start and end tuple.
843 * So we have up to 8 tuples which leads to up to 28 equations
844 * for calculating N_heads and N_sectors. Try to calculate
845 * N_heads and N_sectors from the first possible partition and
846 * if it fails then try also mixed tuples (beginning from first
847 * partition and end from second). Calculation may fail if both
848 * first and last sectors are on cylinder or head boundary
849 * (dividend or divisor is zero). It is possible that different
850 * partitions would have different C/H/S geometry. In this case
851 * we want geometry from the first partition as in most cases
852 * this partition is or was used by BIOS for booting.
853 */
854
855 hh = ss = 0;
856 for (i = 0; i < 4; i++) {
857 p = mbr_get_partition(bufp, i);
858 if (!p->sys_ind)
859 continue;
860
861 c = cylinder(p->bs, p->bc);
862 h = p->bh;
863 s = sector(p->bs);
864 l = dos_partition_get_start(p);
865 chs_set_t(c, h, s, l, t, 2*i);
866
867 sects = dos_partition_get_size(p);
868 if (!sects || lba_overflowed(l, sects))
869 continue;
870
871 c = cylinder(p->es, p->ec);
872 h = p->eh;
873 s = sector(p->es);
874 l += sects-1;
875 chs_set_t(c, h, s, l, t, 2*i+1);
876 }
877
878 for (dif = 1; dif < 8; dif++) {
879 for (i = 0; i + dif < 8; i++) {
880 j = i + dif;
881 if (!t[i].v || !t[j].v)
882 continue;
883 n1 = t[i].o * t[j].h;
884 n2 = t[j].o * t[i].h;
885 n3 = t[j].o * t[i].c;
886 n4 = t[i].o * t[j].c;
887 n5 = t[i].c * t[j].h;
888 n6 = t[j].c * t[i].h;
889 if (!hh && n1 != n2 && n3 != n4) {
890 h = chs_div_minus(n1, n2, n3, n4);
891 if (h > 0 && h <= 256)
892 hh = h;
893 }
894 if (!ss && n3 != n4 && n5 != n6) {
895 s = chs_div_minus(n3, n4, n5, n6);
896 if (s > 0 && s <= 63)
897 ss = s;
898 }
899 if (hh && ss)
900 break;
901 }
902 if (hh && ss)
903 break;
904 }
905
906 if (hh && ss) {
907 *ph = hh;
908 *ps = ss;
909 }
910
911 DBG(LABEL, ul_debug("DOS PT geometry: heads=%u, sectors=%u", *ph, *ps));
912 }
913
914 static int dos_reset_alignment(struct fdisk_context *cxt)
915 {
916 assert(cxt);
917 assert(cxt->label);
918 assert(fdisk_is_label(cxt, DOS));
919
920 /* overwrite necessary stuff by DOS deprecated stuff */
921 if (is_dos_compatible(cxt)) {
922 DBG(LABEL, ul_debug("DOS: resetting alignment for DOS-compatible PT"));
923 if (cxt->geom.sectors)
924 cxt->first_lba = cxt->geom.sectors; /* usually 63 */
925
926 cxt->grain = cxt->sector_size; /* usually 512 */
927 }
928
929 return 0;
930 }
931
932 /* TODO: move to include/pt-dos.h and share with libblkid */
933 #define AIX_MAGIC_STRING "\xC9\xC2\xD4\xC1"
934 #define AIX_MAGIC_STRLEN (sizeof(AIX_MAGIC_STRING) - 1)
935
936 static int dos_probe_label(struct fdisk_context *cxt)
937 {
938 size_t i;
939 unsigned int h = 0, s = 0;
940
941 assert(cxt);
942 assert(cxt->label);
943 assert(fdisk_is_label(cxt, DOS));
944
945 /* ignore disks with AIX magic number */
946 if (memcmp(cxt->firstsector, AIX_MAGIC_STRING, AIX_MAGIC_STRLEN) == 0)
947 return 0;
948
949 if (!mbr_is_valid_magic(cxt->firstsector))
950 return 0;
951
952 dos_init(cxt);
953
954 get_partition_table_geometry(cxt, &h, &s);
955 if (h && s) {
956 cxt->geom.heads = h;
957 cxt->geom.sectors = s;
958 cxt->geom.cylinders = cxt->total_sectors /
959 (cxt->geom.heads * cxt->geom.sectors);
960
961 if (fdisk_has_user_device_geometry(cxt))
962 fdisk_apply_user_device_properties(cxt);
963 }
964
965 for (i = 0; i < 4; i++) {
966 struct pte *pe = self_pte(cxt, i);
967
968 assert(pe);
969 if (is_used_partition(pe->pt_entry))
970 cxt->label->nparts_cur++;
971
972 if (IS_EXTENDED (pe->pt_entry->sys_ind)) {
973 if (cxt->label->nparts_max != 4)
974 fdisk_warnx(cxt, _(
975 "Ignoring extra extended partition %zu"),
976 i + 1);
977 else
978 read_extended(cxt, i);
979 }
980 }
981
982 for (i = 3; i < cxt->label->nparts_max; i++) {
983 struct pte *pe = self_pte(cxt, i);
984 struct fdisk_dos_label *l = self_label(cxt);
985
986 assert(pe);
987 if (!mbr_is_valid_magic(pe->sectorbuffer)) {
988 fdisk_info(cxt, _(
989 "Invalid flag 0x%02x%02x of EBR (for partition %zu) will "
990 "be corrected by w(rite)."),
991 pe->sectorbuffer[510],
992 pe->sectorbuffer[511],
993 i + 1);
994 partition_set_changed(cxt, i, 1);
995
996 /* mark also extended as changed to update the first EBR
997 * in situation that there is no logical partitions at all */
998 partition_set_changed(cxt, l->ext_index, 1);
999 }
1000 }
1001
1002 return 1;
1003 }
1004
1005 static void set_partition(struct fdisk_context *cxt,
1006 int i, int doext, fdisk_sector_t start,
1007 fdisk_sector_t stop, int sysid, int boot)
1008 {
1009 struct pte *pe = self_pte(cxt, i);
1010 struct dos_partition *p;
1011 fdisk_sector_t offset;
1012
1013 assert(!FDISK_IS_UNDEF(start));
1014 assert(!FDISK_IS_UNDEF(stop));
1015 assert(pe);
1016
1017 if (doext) {
1018 struct fdisk_dos_label *l = self_label(cxt);
1019 p = pe->ex_entry;
1020 offset = l->ext_offset;
1021 } else {
1022 p = pe->pt_entry;
1023 offset = pe->offset;
1024 }
1025
1026 DBG(LABEL, ul_debug("DOS: setting partition %d%s, offset=%zu, start=%zu, size=%zu, sysid=%02x",
1027 i, doext ? " [extended]" : "",
1028 (size_t) offset,
1029 (size_t) (start - offset),
1030 (size_t) (stop - start + 1),
1031 sysid));
1032
1033 p->boot_ind = boot ? ACTIVE_FLAG : 0;
1034 p->sys_ind = sysid;
1035 dos_partition_set_start(p, start - offset);
1036 dos_partition_set_size(p, stop - start + 1);
1037 dos_partition_sync_chs(p, offset, cxt->geom.sectors, cxt->geom.heads);
1038 partition_set_changed(cxt, i, 1);
1039 }
1040
1041
1042 static int get_start_from_user( struct fdisk_context *cxt,
1043 fdisk_sector_t *start,
1044 fdisk_sector_t low,
1045 fdisk_sector_t dflt,
1046 fdisk_sector_t limit,
1047 struct fdisk_partition *pa)
1048 {
1049 assert(start);
1050
1051 /* try to use template from 'pa' */
1052 if (pa && pa->start_follow_default)
1053 *start = dflt;
1054
1055 else if (pa && fdisk_partition_has_start(pa)) {
1056 DBG(LABEL, ul_debug("DOS: start: wanted=%ju, low=%ju, limit=%ju",
1057 (uintmax_t) pa->start, (uintmax_t) low, (uintmax_t) limit));
1058 *start = pa->start;
1059 if (*start < low || *start > limit) {
1060 fdisk_warnx(cxt, _("Start sector %ju out of range."),
1061 (uintmax_t) *start);
1062 return -ERANGE;
1063 }
1064 } else {
1065 /* ask user by dialog */
1066 struct fdisk_ask *ask = fdisk_new_ask();
1067 int rc;
1068
1069 if (!ask)
1070 return -ENOMEM;
1071 fdisk_ask_set_query(ask,
1072 fdisk_use_cylinders(cxt) ?
1073 _("First cylinder") : _("First sector"));
1074 fdisk_ask_set_type(ask, FDISK_ASKTYPE_NUMBER);
1075 fdisk_ask_number_set_low(ask, fdisk_cround(cxt, low));
1076 fdisk_ask_number_set_default(ask, fdisk_cround(cxt, dflt));
1077 fdisk_ask_number_set_high(ask, fdisk_cround(cxt, limit));
1078
1079 rc = fdisk_do_ask(cxt, ask);
1080 *start = fdisk_ask_number_get_result(ask);
1081 fdisk_unref_ask(ask);
1082 if (rc)
1083 return rc;
1084 if (fdisk_use_cylinders(cxt)) {
1085 *start = (*start - 1)
1086 * fdisk_get_units_per_sector(cxt);
1087 if (*start < low)
1088 *start = low;
1089 }
1090 }
1091
1092 DBG(LABEL, ul_debug("DOS: start is %ju", (uintmax_t) *start));
1093 return 0;
1094 }
1095
1096 /* Returns last available sector in the free space pointed to by start. */
1097 static int find_last_free(
1098 struct fdisk_context *cxt,
1099 int logical,
1100 fdisk_sector_t begin,
1101 fdisk_sector_t stop,
1102 fdisk_sector_t *result)
1103 {
1104 fdisk_sector_t last = stop;
1105
1106 size_t i = logical ? 4 : 0;
1107
1108 for ( ; i < cxt->label->nparts_max; i++) {
1109 struct pte *pe = self_pte(cxt, i);
1110
1111 assert(pe);
1112 fdisk_sector_t p_start = get_abs_partition_start(pe);
1113 fdisk_sector_t p_end = get_abs_partition_end(pe);
1114
1115 if (is_cleared_partition(pe->pt_entry))
1116 continue;
1117 /* count EBR and begin of the logical partition as used area */
1118 if (pe->offset)
1119 p_start -= cxt->first_lba;
1120
1121 if ((p_start >= begin && p_start <= last) ||
1122 (p_end >= begin && p_end <= last)) {
1123 last = p_start - 1;
1124 }
1125 if (last < begin) {
1126 DBG(LABEL, ul_debug("no free space <%ju,%ju>",
1127 (uintmax_t) begin, (uintmax_t) stop));
1128 return -ENOSPC;
1129 }
1130 }
1131
1132 if (last == begin)
1133 last = stop;
1134
1135 DBG(LABEL, ul_debug("DOS: last free sector <%ju,%ju>: %ju",
1136 (uintmax_t) begin, (uintmax_t) stop, (uintmax_t) last));
1137
1138 *result = last;
1139 return 0;
1140 }
1141
1142 static int find_last_free_sector_in_range(
1143 struct fdisk_context *cxt,
1144 int logical,
1145 fdisk_sector_t begin,
1146 fdisk_sector_t end,
1147 fdisk_sector_t *result)
1148 {
1149 int last_moved;
1150 fdisk_sector_t last = end;
1151
1152 do {
1153 size_t i = logical ? 4 : 0;
1154
1155 last_moved = 0;
1156 for ( ; i < cxt->label->nparts_max; i++) {
1157 struct pte *pe = self_pte(cxt, i);
1158
1159 assert(pe);
1160 fdisk_sector_t p_start = get_abs_partition_start(pe);
1161 fdisk_sector_t p_end = get_abs_partition_end(pe);
1162
1163 if (is_cleared_partition(pe->pt_entry))
1164 continue;
1165
1166 /* count EBR and begin of the logical partition as used area */
1167 if (pe->offset)
1168 p_start -= cxt->first_lba;
1169
1170 if (last >= p_start && last <= p_end) {
1171 last = p_start - 1;
1172 last_moved = 1;
1173
1174 if (last < begin) {
1175 DBG(LABEL, ul_debug("DOS: last free out of range <%ju,%ju>: %ju",
1176 (uintmax_t) begin, (uintmax_t) end, (uintmax_t) last));
1177
1178 return -ENOSPC;
1179 }
1180 }
1181 }
1182 } while (last_moved == 1);
1183
1184 DBG(LABEL, ul_debug("DOS: last unused sector in range <%ju,%ju>: %ju",
1185 (uintmax_t) begin, (uintmax_t) end, (uintmax_t) last));
1186
1187 *result = last;
1188 return 0;
1189 }
1190
1191 static int find_first_free_sector_in_range(
1192 struct fdisk_context *cxt,
1193 int logical,
1194 fdisk_sector_t begin,
1195 fdisk_sector_t end,
1196 fdisk_sector_t *result)
1197 {
1198 int first_moved = 0;
1199 fdisk_sector_t first = begin;
1200
1201 do {
1202 size_t i = logical ? 4 : 0;
1203
1204 first_moved = 0;
1205 for (; i < cxt->label->nparts_max; i++) {
1206 struct pte *pe = self_pte(cxt, i);
1207
1208 assert(pe);
1209 fdisk_sector_t p_start = get_abs_partition_start(pe);
1210 fdisk_sector_t p_end = get_abs_partition_end(pe);
1211
1212 if (is_cleared_partition(pe->pt_entry))
1213 continue;
1214 /* count EBR and begin of the logical partition as used area */
1215 if (pe->offset)
1216 p_start -= cxt->first_lba;
1217 if (first < p_start)
1218 continue;
1219
1220 /* if we're placing a logical partition start, ensure
1221 there's room for the *next* EBR (stored at start - first_lba).
1222 therefore enforce:
1223 start(Lnew) >= end(Lprev) + first_lba + 1 */
1224 if ((logical && first > p_end && (first - p_end) <= cxt->first_lba)
1225 || first <= p_end) {
1226
1227 first = p_end + 1 + (logical ? cxt->first_lba : 0);
1228 first_moved = 1;
1229
1230 if (first > end) {
1231 DBG(LABEL, ul_debug("DOS: first free out of range <%ju,%ju>: %ju",
1232 (uintmax_t) begin, (uintmax_t) end, (uintmax_t) first));
1233 return -ENOSPC;
1234 }
1235 }
1236 }
1237 } while (first_moved == 1);
1238
1239 DBG(LABEL, ul_debug("DOS: first unused sector in range <%ju,%ju>: %ju",
1240 (uintmax_t) begin, (uintmax_t) end, (uintmax_t) first));
1241 *result = first;
1242 return 0;
1243 }
1244
1245 static int get_disk_ranges(struct fdisk_context *cxt, int logical,
1246 fdisk_sector_t *first, fdisk_sector_t *last)
1247 {
1248 if (logical) {
1249 /* logical partitions */
1250 struct fdisk_dos_label *l = self_label(cxt);
1251 struct pte *ext_pe = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL;
1252
1253 if (!ext_pe)
1254 return -EINVAL;
1255
1256 *first = l->ext_offset + cxt->first_lba;
1257 *last = get_abs_partition_end(ext_pe);
1258
1259 } else {
1260 /* primary partitions */
1261 if (fdisk_use_cylinders(cxt) || !cxt->total_sectors)
1262 *last = cxt->geom.heads * cxt->geom.sectors * cxt->geom.cylinders - 1;
1263 else
1264 *last = cxt->total_sectors - 1;
1265
1266 if (*last >= UINT_MAX)
1267 *last = UINT_MAX - 1;
1268 *first = cxt->first_lba;
1269 }
1270
1271 return 0;
1272 }
1273
1274 /* first free sector on disk */
1275 static int find_first_free_sector(struct fdisk_context *cxt,
1276 int logical,
1277 fdisk_sector_t start,
1278 fdisk_sector_t *result)
1279 {
1280 fdisk_sector_t first, last;
1281 int rc;
1282
1283 rc = get_disk_ranges(cxt, logical, &first, &last);
1284 if (rc)
1285 return rc;
1286
1287 return find_first_free_sector_in_range(cxt, logical, start, last, result);
1288 }
1289
1290 static int add_partition(struct fdisk_context *cxt, size_t n,
1291 struct fdisk_partition *pa)
1292 {
1293 int sys, read = 0, rc, isrel = 0, is_logical;
1294 struct fdisk_dos_label *l = self_label(cxt);
1295 struct dos_partition *p = self_partition(cxt, n);
1296 struct fdisk_ask *ask = NULL;
1297
1298 fdisk_sector_t start, stop, limit, temp;
1299
1300 DBG(LABEL, ul_debug("DOS: adding partition %zu", n));
1301
1302 sys = pa && pa->type ? pa->type->code : MBR_LINUX_DATA_PARTITION;
1303 is_logical = n >= 4;
1304
1305 if (p && is_used_partition(p)) {
1306 fdisk_warnx(cxt, _("Partition %zu is already defined. "
1307 "Delete it before re-adding it."),
1308 n + 1);
1309 return -EINVAL;
1310 }
1311
1312 rc = get_disk_ranges(cxt, is_logical, &start, &stop);
1313 if (rc)
1314 return rc;
1315
1316 if (!is_logical && cxt->parent && fdisk_is_label(cxt->parent, GPT))
1317 start = 1; /* Bad boy modifies hybrid MBR */
1318
1319 rc = find_last_free_sector_in_range(cxt, is_logical, start, stop, &limit);
1320 if (rc == -ENOSPC)
1321 fdisk_warnx(cxt, _("No free sectors available."));
1322 if (rc)
1323 return rc;
1324
1325 if ((is_logical || !cxt->parent || !fdisk_is_label(cxt->parent, GPT))
1326 && cxt->script && pa && fdisk_partition_has_start(pa)
1327 && pa->start >= (is_logical ? l->ext_offset : 1)
1328 && pa->start < start) {
1329 fdisk_set_first_lba(cxt, 1);
1330
1331 rc = get_disk_ranges(cxt, is_logical, &start, &stop);
1332 if (rc) /* won't happen, but checking to be proper */
1333 return rc;
1334 }
1335
1336 /*
1337 * Ask for first sector
1338 */
1339 do {
1340 fdisk_sector_t dflt, aligned;
1341
1342 temp = start;
1343
1344 DBG(LABEL, ul_debug("DOS: >>> search for first free from %ju", start));
1345 rc = find_first_free_sector(cxt, is_logical, start, &dflt);
1346 if (rc == -ENOSPC)
1347 fdisk_warnx(cxt, _("No free sectors available."));
1348 if (rc)
1349 return rc;
1350 start = dflt;
1351
1352 if (n >= 4 && pa && fdisk_partition_has_start(pa) && cxt->script
1353 && cxt->first_lba > 1
1354 && temp == start - cxt->first_lba) {
1355 fdisk_set_first_lba(cxt, 1);
1356 start = pa->start;
1357 }
1358
1359 /* the default sector should be aligned and unused */
1360 do {
1361 aligned = fdisk_align_lba_in_range(cxt, dflt, dflt, limit);
1362 find_first_free_sector(cxt, is_logical, aligned, &dflt);
1363 } while (dflt != aligned && dflt > aligned && dflt < limit);
1364
1365 if (dflt >= limit)
1366 dflt = start;
1367 if (start > limit)
1368 break;
1369 if (start >= temp + fdisk_get_units_per_sector(cxt)
1370 && read) {
1371 if (!pa || !pa->start_follow_default)
1372 fdisk_info(cxt, _("Sector %ju is already allocated."),
1373 (uintmax_t) temp);
1374 temp = start;
1375 read = 0;
1376 if (pa && fdisk_partition_has_start(pa))
1377 break;
1378 }
1379
1380 if (!read && start == temp) {
1381 rc = get_start_from_user(cxt, &start, temp, dflt, limit, pa);
1382 if (rc)
1383 return rc;
1384 read = 1;
1385 }
1386 if (pa && fdisk_partition_has_size(pa)) {
1387 fdisk_sector_t last;
1388
1389 rc = find_last_free(cxt, is_logical, start, limit, &last);
1390 if (rc == 0 && last - start + 1 < fdisk_partition_get_size(pa)) {
1391 DBG(LABEL, ul_debug("DOS: area <%ju,%ju> too small [wanted=%ju aval=%ju]",
1392 (uintmax_t) start, (uintmax_t) last,
1393 fdisk_partition_get_size(pa),
1394 last - start));
1395
1396 if (fdisk_partition_has_start(pa)
1397 && fdisk_partition_get_start(pa) <= last)
1398 rc = -ENOSPC;
1399 else {
1400 start = last + 1;
1401 continue;
1402 }
1403 }
1404 if (rc == -ENOSPC) {
1405 fdisk_warnx(cxt, _("No free sectors available."));
1406 return rc;
1407 }
1408 }
1409
1410 } while (start != temp || !read);
1411
1412 if (n == 4) {
1413 /* The first EBR is stored at begin of the extended partition */
1414 struct pte *pe = self_pte(cxt, n);
1415
1416 assert(pe);
1417 pe->offset = l->ext_offset;
1418 } else if (n > 4) {
1419 /* The second (and another) EBR */
1420 struct pte *pe = self_pte(cxt, n);
1421
1422 assert(pe);
1423 assert(start >= cxt->first_lba);
1424
1425 pe->offset = start - cxt->first_lba;
1426 DBG(LABEL, ul_debug("DOS: setting EBR offset to %ju [start=%ju]", pe->offset, start));
1427
1428 if (pe->offset == l->ext_offset) { /* must be corrected */
1429 pe->offset++;
1430 if (cxt->first_lba == 1)
1431 start++;
1432 }
1433 }
1434
1435 rc = find_last_free(cxt, is_logical, start, limit, &stop);
1436 if (rc == -ENOSPC)
1437 fdisk_warnx(cxt, _("No free sectors available."));
1438 if (rc)
1439 return rc;
1440 limit = stop;
1441
1442 /*
1443 * Ask for last sector
1444 */
1445 if (fdisk_cround(cxt, start) == fdisk_cround(cxt, limit))
1446 stop = limit;
1447 else if (pa && pa->end_follow_default)
1448 stop = limit;
1449 else if (pa && fdisk_partition_has_size(pa)) {
1450 stop = start + pa->size;
1451 isrel = pa->size_explicit ? 0 : 1;
1452 if ((!isrel || !alignment_required(cxt)) && stop > start)
1453 stop -= 1;
1454 } else {
1455 /* ask user by dialog */
1456 for (;;) {
1457 if (!ask)
1458 ask = fdisk_new_ask();
1459 else
1460 fdisk_reset_ask(ask);
1461 if (!ask)
1462 return -ENOMEM;
1463 fdisk_ask_set_type(ask, FDISK_ASKTYPE_OFFSET);
1464
1465 if (fdisk_use_cylinders(cxt)) {
1466 fdisk_ask_set_query(ask, _("Last cylinder, +/-cylinders or +/-size{K,M,G,T,P}"));
1467 fdisk_ask_number_set_unit(ask,
1468 cxt->sector_size *
1469 fdisk_get_units_per_sector(cxt));
1470 } else {
1471 fdisk_ask_set_query(ask, _("Last sector, +/-sectors or +/-size{K,M,G,T,P}"));
1472 fdisk_ask_number_set_unit(ask,cxt->sector_size);
1473 }
1474
1475 fdisk_ask_number_set_low(ask, fdisk_cround(cxt, start));
1476 fdisk_ask_number_set_default(ask, fdisk_cround(cxt, limit));
1477 fdisk_ask_number_set_high(ask, fdisk_cround(cxt, limit));
1478 fdisk_ask_number_set_base(ask, fdisk_cround(cxt, start)); /* base for relative input */
1479 fdisk_ask_number_set_wrap_negative(ask, 1); /* wrap negative around high */
1480
1481 rc = fdisk_do_ask(cxt, ask);
1482 if (rc)
1483 goto done;
1484
1485 stop = fdisk_ask_number_get_result(ask);
1486 isrel = fdisk_ask_number_is_relative(ask);
1487 if (fdisk_use_cylinders(cxt)) {
1488 stop = stop * fdisk_get_units_per_sector(cxt) - 1;
1489 if (stop >limit)
1490 stop = limit;
1491 }
1492
1493 if (stop >= start && stop <= limit)
1494 break;
1495 fdisk_warnx(cxt, _("Value out of range."));
1496 }
1497 }
1498
1499 DBG(LABEL, ul_debug("DOS: raw stop: %ju [limit %ju]", (uintmax_t) stop, (uintmax_t) limit));
1500
1501 if (stop > limit)
1502 stop = limit;
1503
1504 if (isrel && stop - start < (cxt->grain / fdisk_get_sector_size(cxt))) {
1505 /* Don't try to be smart on very small partitions and don't align so small sizes */
1506 isrel = 0;
1507 DBG(LABEL, ul_debug("DOS: don't align end of tiny partition [start=%ju, stop=%ju, grain=%lu]",
1508 (uintmax_t)start, (uintmax_t)stop, cxt->grain));
1509 }
1510
1511 if (stop < limit && isrel && alignment_required(cxt)) {
1512 /* the last sector has not been exactly requested (but
1513 * defined by +size{K,M,G} convention), so be smart and
1514 * align the end of the partition. The next partition
1515 * will start at phy.block boundary.
1516 */
1517 stop = fdisk_align_lba_in_range(cxt, stop, start, limit);
1518 if (stop > start)
1519 stop -= 1; /* end one sector before aligned offset */
1520 if (stop > limit)
1521 stop = limit;
1522 DBG(LABEL, ul_debug("DOS: aligned stop: %ju", (uintmax_t) stop));
1523 }
1524
1525 set_partition(cxt, n, 0, start, stop, sys, fdisk_partition_is_bootable(pa));
1526 if (n > 4) {
1527 struct pte *pe = self_pte(cxt, n);
1528 assert(pe);
1529 set_partition(cxt, n - 1, 1, pe->offset, stop,
1530 MBR_DOS_EXTENDED_PARTITION, 0);
1531 }
1532
1533 /* report */
1534 {
1535 struct fdisk_parttype *t =
1536 fdisk_label_get_parttype_from_code(cxt->label, sys);
1537 fdisk_info_new_partition(cxt, n + 1, start, stop, t);
1538 fdisk_unref_parttype(t);
1539 }
1540
1541
1542 if (IS_EXTENDED(sys)) {
1543 struct pte *pen = self_pte(cxt, n);
1544
1545 assert(pen);
1546 l->ext_index = n;
1547 l->ext_offset = start;
1548 pen->ex_entry = p;
1549 }
1550
1551 fdisk_label_set_changed(cxt->label, 1);
1552 rc = 0;
1553 done:
1554 fdisk_unref_ask(ask);
1555 return rc;
1556 }
1557
1558 static int add_logical(struct fdisk_context *cxt,
1559 struct fdisk_partition *pa,
1560 size_t *partno)
1561 {
1562 struct pte *pe;
1563 int rc;
1564
1565 assert(cxt);
1566 assert(partno);
1567 assert(cxt->label);
1568 assert(self_label(cxt)->ext_offset);
1569
1570 DBG(LABEL, ul_debug("DOS: nparts max: %zu", cxt->label->nparts_max));
1571 pe = self_pte(cxt, cxt->label->nparts_max);
1572 assert(pe);
1573
1574 if (!pe->sectorbuffer) {
1575 pe->sectorbuffer = calloc(1, cxt->sector_size);
1576 if (!pe->sectorbuffer)
1577 return -ENOMEM;
1578 DBG(LABEL, ul_debug("DOS: logical: %zu: new EBR sector buffer %p",
1579 cxt->label->nparts_max, pe->sectorbuffer));
1580 pe->private_sectorbuffer = 1;
1581 }
1582 pe->pt_entry = mbr_get_partition(pe->sectorbuffer, 0);
1583 pe->ex_entry = pe->pt_entry + 1;
1584 pe->offset = 0;
1585 partition_set_changed(cxt, cxt->label->nparts_max, 1);
1586
1587 cxt->label->nparts_max++;
1588
1589 /* this message makes sense only when we use extended/primary/logical
1590 * dialog. The dialog is disable for scripts, see dos_add_partition() */
1591 if (!cxt->script)
1592 fdisk_info(cxt, _("Adding logical partition %zu"),
1593 cxt->label->nparts_max);
1594 *partno = cxt->label->nparts_max - 1;
1595 rc = add_partition(cxt, *partno, pa);
1596
1597 if (rc) {
1598 /* reset on error */
1599 cxt->label->nparts_max--;
1600 pe->pt_entry = NULL;
1601 pe->ex_entry = NULL;
1602 pe->offset = 0;
1603 pe->changed = 0;
1604 }
1605
1606 return rc;
1607 }
1608
1609 static int check(struct fdisk_context *cxt, size_t n,
1610 unsigned int h, unsigned int s, unsigned int c,
1611 unsigned int lba_sector)
1612 {
1613 unsigned int chs_sector, real_s, real_c;
1614 int nerrors = 0;
1615
1616 if (!is_dos_compatible(cxt))
1617 return 0;
1618
1619 real_s = sector(s) - 1;
1620 real_c = cylinder(s, c);
1621 chs_sector = (real_c * cxt->geom.heads + h) * cxt->geom.sectors + real_s;
1622
1623 if (!chs_sector) {
1624 fdisk_warnx(cxt, _("Partition %zu: contains sector 0"), n);
1625 nerrors++;
1626 }
1627 if (h >= cxt->geom.heads) {
1628 fdisk_warnx(cxt, _("Partition %zu: head %d greater than "
1629 "maximum %d"), n, h + 1, cxt->geom.heads);
1630 nerrors++;
1631 }
1632 if (real_s >= cxt->geom.sectors) {
1633 fdisk_warnx(cxt, _("Partition %zu: sector %d greater than "
1634 "maximum %ju"), n, real_s + 1,
1635 (uintmax_t) cxt->geom.sectors);
1636 nerrors++;
1637 }
1638 if (real_c >= cxt->geom.cylinders) {
1639 fdisk_warnx(cxt, _("Partition %zu: cylinder %d greater than "
1640 "maximum %ju"),
1641 n, real_c + 1,
1642 (uintmax_t) cxt->geom.cylinders);
1643 nerrors++;
1644 }
1645 if (lba_sector / (cxt->geom.heads * cxt->geom.sectors) < 1024 && lba_sector != chs_sector) {
1646 fdisk_warnx(cxt, _("Partition %zu: LBA sector %u "
1647 "disagrees with C/H/S calculated sector %u"),
1648 n, lba_sector, chs_sector);
1649 nerrors++;
1650 }
1651
1652 return nerrors;
1653 }
1654
1655 /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1656 * [email protected], based on code fragments from pfdisk by Gordon W. Ross,
1657 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1658 * Lubkin Oct. 1991). */
1659
1660 static void
1661 long2chs(struct fdisk_context *cxt, unsigned long ls,
1662 unsigned int *c, unsigned int *h, unsigned int *s) {
1663 int spc = cxt->geom.heads * cxt->geom.sectors;
1664
1665 *c = ls / spc;
1666 ls = ls % spc;
1667 *h = ls / cxt->geom.sectors;
1668 *s = ls % cxt->geom.sectors + 1; /* sectors count from 1 */
1669 }
1670
1671 static int check_consistency(struct fdisk_context *cxt, struct dos_partition *p,
1672 size_t partition)
1673 {
1674 unsigned int pbc, pbh, pbs; /* physical beginning c, h, s */
1675 unsigned int pec, peh, pes; /* physical ending c, h, s */
1676 unsigned int lbc, lbh, lbs; /* logical beginning c, h, s */
1677 unsigned int lec, leh, les; /* logical ending c, h, s */
1678 int nerrors = 0;
1679
1680 if (!is_dos_compatible(cxt))
1681 return 0;
1682
1683 if (!cxt->geom.heads || !cxt->geom.sectors || (partition >= 4))
1684 return 0; /* do not check extended partitions */
1685
1686 /* physical beginning c, h, s */
1687 pbc = cylinder(p->bs, p->bc);
1688 pbh = p->bh;
1689 pbs = sector(p->bs);
1690
1691 /* physical ending c, h, s */
1692 pec = cylinder(p->es, p->ec);
1693 peh = p->eh;
1694 pes = sector(p->es);
1695
1696 /* compute logical beginning (c, h, s) */
1697 long2chs(cxt, dos_partition_get_start(p), &lbc, &lbh, &lbs);
1698
1699 /* compute logical ending (c, h, s) */
1700 long2chs(cxt, dos_partition_get_start(p) + dos_partition_get_size(p) - 1, &lec, &leh, &les);
1701
1702 /* Same physical / logical beginning? */
1703 if (lbc < 1024
1704 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1705 fdisk_warnx(cxt, _("Partition %zu: different physical/logical "
1706 "beginnings (non-Linux?): "
1707 "phys=(%d, %d, %d), logical=(%d, %d, %d)"),
1708 partition + 1,
1709 pbc, pbh, pbs,
1710 lbc, lbh, lbs);
1711 nerrors++;
1712 }
1713
1714 /* Same physical / logical ending? */
1715 if (lec < 1024
1716 && (pec != lec || peh != leh || pes != les)) {
1717 fdisk_warnx(cxt, _("Partition %zu: different physical/logical "
1718 "endings: phys=(%d, %d, %d), logical=(%d, %d, %d)"),
1719 partition + 1,
1720 pec, peh, pes,
1721 lec, leh, les);
1722 nerrors++;
1723 }
1724
1725 /* Ending on cylinder boundary? */
1726 if (peh != (cxt->geom.heads - 1) || pes != cxt->geom.sectors) {
1727 fdisk_warnx(cxt, _("Partition %zu: does not end on "
1728 "cylinder boundary."),
1729 partition + 1);
1730 nerrors++;
1731 }
1732
1733 return nerrors;
1734 }
1735
1736 static void fill_bounds(struct fdisk_context *cxt,
1737 fdisk_sector_t *first, fdisk_sector_t *last)
1738 {
1739 size_t i;
1740 struct pte *pe = self_pte(cxt, 0);
1741 struct dos_partition *p;
1742
1743 assert(pe);
1744 for (i = 0; i < cxt->label->nparts_max; pe++,i++) {
1745 p = pe->pt_entry;
1746 if (is_cleared_partition(p) || IS_EXTENDED (p->sys_ind)) {
1747 first[i] = SIZE_MAX;
1748 last[i] = 0;
1749 } else {
1750 first[i] = get_abs_partition_start(pe);
1751 last[i] = get_abs_partition_end(pe);
1752 }
1753 }
1754 }
1755
1756 static int dos_verify_disklabel(struct fdisk_context *cxt)
1757 {
1758 size_t i, j;
1759 fdisk_sector_t total = 1, n_sectors = cxt->total_sectors;
1760 fdisk_sector_t *first, *last;
1761 struct dos_partition *p;
1762 struct fdisk_dos_label *l = self_label(cxt);
1763 int nerrors = 0;
1764
1765 assert(fdisk_is_label(cxt, DOS));
1766
1767 first = calloc(cxt->label->nparts_max, sizeof(*first));
1768 last = calloc(cxt->label->nparts_max, sizeof(*first));
1769
1770 if (!first || !last) {
1771 free(first);
1772 free(last);
1773 return -ENOMEM;
1774 }
1775
1776 fill_bounds(cxt, first, last);
1777 for (i = 0; i < cxt->label->nparts_max; i++) {
1778 struct pte *pe = self_pte(cxt, i);
1779
1780 p = self_partition(cxt, i);
1781 if (p && is_used_partition(p) && !IS_EXTENDED(p->sys_ind)) {
1782 nerrors += check_consistency(cxt, p, i);
1783 assert(pe);
1784 if (get_abs_partition_start(pe) < first[i]) {
1785 fdisk_warnx(cxt, _(
1786 "Partition %zu: bad start-of-data."),
1787 i + 1);
1788 nerrors++;
1789 }
1790
1791 nerrors += check(cxt, i + 1, p->bh, p->bs, p->bc, first[i]);
1792 nerrors += check(cxt, i + 1, p->eh, p->es, p->ec, last[i]);
1793 total += last[i] + 1 - first[i];
1794
1795 if (i == 0)
1796 total += get_abs_partition_start(pe) - 1;
1797
1798 for (j = 0; j < i; j++) {
1799 if ((first[i] >= first[j] && first[i] <= last[j])
1800 || ((last[i] <= last[j] && last[i] >= first[j]))) {
1801
1802 fdisk_warnx(cxt, _("Partition %zu: "
1803 "overlaps partition %zu."),
1804 j + 1, i + 1);
1805 nerrors++;
1806
1807 total += first[i] >= first[j] ?
1808 first[i] : first[j];
1809 total -= last[i] <= last[j] ?
1810 last[i] : last[j];
1811 }
1812 }
1813 }
1814 }
1815
1816 if (l->ext_offset) {
1817 fdisk_sector_t e_last;
1818 struct pte *ext_pe = self_pte(cxt, l->ext_index);
1819
1820 assert(ext_pe);
1821 e_last = get_abs_partition_end(ext_pe);
1822
1823 for (i = 4; i < cxt->label->nparts_max; i++) {
1824 total++;
1825 p = self_partition(cxt, i);
1826 assert(p);
1827
1828 if (!p->sys_ind) {
1829 if (i != 4 || i + 1 < cxt->label->nparts_max) {
1830 fdisk_warnx(cxt,
1831 _("Partition %zu: empty."),
1832 i + 1);
1833 nerrors++;
1834 }
1835 } else if (first[i] < l->ext_offset
1836 || last[i] > e_last) {
1837
1838 fdisk_warnx(cxt, _("Logical partition %zu: "
1839 "not entirely in partition %zu."),
1840 i + 1, l->ext_index + 1);
1841 nerrors++;
1842 }
1843 }
1844 }
1845
1846 if (!nerrors) {
1847 fdisk_info(cxt, _("No errors detected."));
1848 if (total > n_sectors)
1849 fdisk_info(cxt, _("Total allocated sectors %ju greater "
1850 "than the maximum %ju."), (uintmax_t) total, (uintmax_t) n_sectors);
1851 else if (total < n_sectors)
1852 fdisk_info(cxt, _("Remaining %ju unallocated %ld-byte "
1853 "sectors."), (uintmax_t) n_sectors - total, cxt->sector_size);
1854 } else
1855 fdisk_warnx(cxt,
1856 P_("%d error detected.", "%d errors detected.", nerrors),
1857 nerrors);
1858
1859 free(first);
1860 free(last);
1861 return nerrors;
1862 }
1863
1864 /*
1865 * Ask the user for new partition type information (logical, extended).
1866 * This function calls the actual partition adding logic - add_partition.
1867 *
1868 * API callback.
1869 */
1870 static int dos_add_partition(struct fdisk_context *cxt,
1871 struct fdisk_partition *pa,
1872 size_t *partno)
1873 {
1874 size_t i;
1875 uint8_t free_primary = 0, free_sectors = 0;
1876 fdisk_sector_t first = 0, grain;
1877 int rc = 0;
1878 struct fdisk_dos_label *l;
1879 struct pte *ext_pe;
1880 size_t res = 0; /* partno */
1881
1882 assert(cxt);
1883 assert(cxt->label);
1884 assert(fdisk_is_label(cxt, DOS));
1885
1886 DBG(LABEL, ul_debug("DOS: new partition wanted"));
1887
1888 l = self_label(cxt);
1889
1890 if (cxt->label->nparts_max >= MAXIMUM_PARTS) {
1891 fdisk_warnx(cxt, _("The maximum number of partitions has "
1892 "been created."));
1893 return -EINVAL;
1894 }
1895
1896 ext_pe = l->ext_offset ? self_pte(cxt, l->ext_index) : NULL;
1897
1898 /*
1899 * partition template (@pa) based partitioning
1900 */
1901
1902 /* A) template specifies start within extended partition; add logical */
1903 if (pa && fdisk_partition_has_start(pa) && ext_pe
1904 && pa->start >= l->ext_offset
1905 && pa->start <= get_abs_partition_end(ext_pe)) {
1906 DBG(LABEL, ul_debug("DOS: pa template %p: add logical (by offset)", pa));
1907
1908 if (fdisk_partition_has_partno(pa) && fdisk_partition_get_partno(pa) < 4) {
1909 DBG(LABEL, ul_debug("DOS: pa template specifies partno<4 for logical partition"));
1910 return -EINVAL;
1911 }
1912 rc = add_logical(cxt, pa, &res);
1913 goto done;
1914
1915 /* B) template specifies start out of extended partition; add primary */
1916 } else if (pa && fdisk_partition_has_start(pa) && ext_pe) {
1917 DBG(LABEL, ul_debug("DOS: pa template %p: add primary (by offset)", pa));
1918
1919 if (fdisk_partition_has_partno(pa) && fdisk_partition_get_partno(pa) >= 4) {
1920 DBG(LABEL, ul_debug("DOS: pa template specifies partno>=4 for primary partition"));
1921 return -EINVAL;
1922 }
1923 if (ext_pe && pa->type && IS_EXTENDED(pa->type->code)) {
1924 fdisk_warnx(cxt, _("Extended partition already exists."));
1925 return -EINVAL;
1926 }
1927 rc = get_partition_unused_primary(cxt, pa, &res);
1928 if (rc == 0)
1929 rc = add_partition(cxt, res, pa);
1930 goto done;
1931
1932 /* C) template specifies start (or default), partno < 4; add primary */
1933 } else if (pa && (fdisk_partition_start_is_default(pa) || fdisk_partition_has_start(pa))
1934 && fdisk_partition_has_partno(pa)
1935 && pa->partno < 4) {
1936 DBG(LABEL, ul_debug("DOS: pa template %p: add primary (by partno)", pa));
1937
1938 if (ext_pe && pa->type && IS_EXTENDED(pa->type->code)) {
1939 fdisk_warnx(cxt, _("Extended partition already exists."));
1940 return -EINVAL;
1941 }
1942 rc = get_partition_unused_primary(cxt, pa, &res);
1943 if (rc == 0)
1944 rc = add_partition(cxt, res, pa);
1945 goto done;
1946
1947 /* D) template specifies start (or default), partno >= 4; add logical */
1948 } else if (pa && (fdisk_partition_start_is_default(pa) || fdisk_partition_has_start(pa))
1949 && fdisk_partition_has_partno(pa)
1950 && pa->partno >= 4) {
1951 DBG(LABEL, ul_debug("DOS: pa template %p: add logical (by partno)", pa));
1952
1953 if (!ext_pe) {
1954 fdisk_warnx(cxt, _("Extended partition does not exists. Failed to add logical partition."));
1955 return -EINVAL;
1956 }
1957
1958 if (fdisk_partition_has_start(pa)
1959 && pa->start < l->ext_offset
1960 && pa->start > get_abs_partition_end(ext_pe)) {
1961 DBG(LABEL, ul_debug("DOS: pa template specifies partno>=4, but start out of extended"));
1962 return -EINVAL;
1963 }
1964
1965 rc = add_logical(cxt, pa, &res);
1966 goto done;
1967 }
1968
1969 DBG(LABEL, ul_debug("DOS: dialog driven partitioning"));
1970 /* Note @pa may be still used for things like partition type, etc */
1971
1972 /* check if there is space for primary partition */
1973 grain = cxt->grain > cxt->sector_size ? cxt->grain / cxt->sector_size : 1;
1974 first = cxt->first_lba;
1975
1976 if (cxt->parent && fdisk_is_label(cxt->parent, GPT)) {
1977 /* modifying a hybrid MBR, which throws out the rules */
1978 grain = 1;
1979 first = 1;
1980 }
1981
1982 /* set @first after the last used partition, set @free_sectors if there
1983 * is gap in front if the first partition or between used parrtitions. */
1984 for (i = 0; i < 4; i++) {
1985 struct dos_partition *p = self_partition(cxt, i);
1986
1987 if (p && is_used_partition(p)) {
1988 fdisk_sector_t start = dos_partition_get_start(p);
1989 if (first + grain <= start)
1990 free_sectors = 1;
1991 first = start + dos_partition_get_size(p);
1992 } else
1993 free_primary++;
1994 }
1995
1996 /* set @free_sectors if there is a space after the first usable sector */
1997 if (first + grain - 1 <= cxt->total_sectors - 1)
1998 free_sectors = 1;
1999
2000 DBG(LABEL, ul_debug("DOS: primary: first free: %ju, last on disk: %ju, "
2001 "free_sectors=%d, free_primary=%d",
2002 (uintmax_t) first,
2003 (uintmax_t) cxt->total_sectors - 1,
2004 free_sectors, free_primary));
2005
2006 if (!free_primary || !free_sectors) {
2007 DBG(LABEL, ul_debug("DOS: primary impossible"));
2008 if (l->ext_offset) {
2009 if (!pa || fdisk_partition_has_start(pa)) {
2010 /* See above case A); here we have start, but
2011 * out of extended partition */
2012 const char *msg;
2013 if (!free_primary)
2014 msg = _("All primary partitions are in use.");
2015 else
2016 msg = _("All space for primary partitions is in use.");
2017
2018 if (pa && fdisk_partition_has_start(pa)) {
2019 fdisk_warnx(cxt, "%s", msg);
2020 return -EINVAL;
2021 }
2022 fdisk_info(cxt, "%s", msg);
2023 }
2024 DBG(LABEL, ul_debug("DOS: trying logical"));
2025 rc = add_logical(cxt, pa, &res);
2026 } else {
2027 if (free_primary)
2028 fdisk_info(cxt, _("All space for primary partitions is in use."));
2029 else
2030 /* TRANSLATORS: Try to keep this within 80 characters. */
2031 fdisk_info(cxt, _("To create more partitions, first replace "
2032 "a primary with an extended partition."));
2033 return -EINVAL;
2034 }
2035 } else {
2036 char hint[BUFSIZ];
2037 struct fdisk_ask *ask;
2038 int c = 0;
2039
2040 /* the default layout for scripts is to create primary partitions */
2041 if (cxt->script || !fdisk_has_dialogs(cxt)) {
2042 rc = get_partition_unused_primary(cxt, pa, &res);
2043 if (rc == 0)
2044 rc = add_partition(cxt, res, pa);
2045 goto done;
2046 }
2047
2048 ask = fdisk_new_ask();
2049 if (!ask)
2050 return -ENOMEM;
2051 fdisk_ask_set_type(ask, FDISK_ASKTYPE_MENU);
2052 fdisk_ask_set_query(ask, _("Partition type"));
2053 fdisk_ask_menu_set_default(ask, free_primary == 1
2054 && !l->ext_offset ? 'e' : 'p');
2055 snprintf(hint, sizeof(hint),
2056 _("%u primary, %d extended, %u free"),
2057 4 - (l->ext_offset ? 1 : 0) - free_primary,
2058 l->ext_offset ? 1 : 0,
2059 free_primary);
2060
2061 fdisk_ask_menu_add_item(ask, 'p', _("primary"), hint);
2062 if (!l->ext_offset)
2063 fdisk_ask_menu_add_item(ask, 'e', _("extended"), _("container for logical partitions"));
2064 else
2065 fdisk_ask_menu_add_item(ask, 'l', _("logical"), _("numbered from 5"));
2066
2067 rc = fdisk_do_ask(cxt, ask);
2068 if (!rc)
2069 fdisk_ask_menu_get_result(ask, &c);
2070 fdisk_unref_ask(ask);
2071 if (rc)
2072 return rc;
2073
2074 if (c == 'p') {
2075 rc = get_partition_unused_primary(cxt, pa, &res);
2076 if (rc == 0)
2077 rc = add_partition(cxt, res, pa);
2078 goto done;
2079 } else if (c == 'l' && l->ext_offset) {
2080 rc = add_logical(cxt, pa, &res);
2081 goto done;
2082 } else if (c == 'e' && !l->ext_offset) {
2083 rc = get_partition_unused_primary(cxt, pa, &res);
2084 if (rc == 0) {
2085 struct fdisk_partition *xpa = NULL;
2086 struct fdisk_parttype *t;
2087
2088 t = fdisk_label_get_parttype_from_code(cxt->label,
2089 MBR_DOS_EXTENDED_PARTITION);
2090 if (!pa) {
2091 pa = xpa = fdisk_new_partition();
2092 if (!xpa)
2093 return -ENOMEM;
2094 }
2095 fdisk_partition_set_type(pa, t);
2096 rc = add_partition(cxt, res, pa);
2097 if (xpa) {
2098 fdisk_unref_partition(xpa);
2099 pa = NULL;
2100 }
2101 }
2102 goto done;
2103 } else
2104 fdisk_warnx(cxt, _("Invalid partition type `%c'."), c);
2105 }
2106 done:
2107 if (rc == 0) {
2108 cxt->label->nparts_cur++;
2109 if (partno)
2110 *partno = res;
2111 }
2112 return rc;
2113 }
2114
2115 static int write_sector(struct fdisk_context *cxt, fdisk_sector_t secno,
2116 unsigned char *buf)
2117 {
2118 int rc;
2119
2120 rc = seek_sector(cxt, secno);
2121 if (rc != 0) {
2122 fdisk_warn(cxt, _("Cannot write sector %jd: seek failed"),
2123 (uintmax_t) secno);
2124 return rc;
2125 }
2126
2127 DBG(LABEL, ul_debug("DOS: writing to sector %ju", (uintmax_t) secno));
2128
2129 if (write(cxt->dev_fd, buf, cxt->sector_size) != (ssize_t) cxt->sector_size)
2130 return -errno;
2131 return 0;
2132 }
2133
2134 static int dos_write_disklabel(struct fdisk_context *cxt)
2135 {
2136 struct fdisk_dos_label *l = self_label(cxt);
2137 size_t i;
2138 int rc = 0, mbr_changed = 0;
2139
2140 assert(cxt);
2141 assert(cxt->label);
2142 assert(fdisk_is_label(cxt, DOS));
2143
2144 DBG(LABEL, ul_debug("DOS: write PT requested [label-changed: %d, non-pt-changed: %d]",
2145 cxt->label->changed, l->non_pt_changed));
2146
2147 mbr_changed = l->non_pt_changed;
2148
2149 /* MBR (primary partitions) */
2150 if (!mbr_changed) {
2151 for (i = 0; i < 4; i++) {
2152 struct pte *pe = self_pte(cxt, i);
2153
2154 assert(pe);
2155 if (pe->changed)
2156 mbr_changed = 1;
2157 }
2158 }
2159 if (mbr_changed) {
2160 DBG(LABEL, ul_debug("DOS: MBR changed, writing"));
2161 mbr_set_magic(cxt->firstsector);
2162 rc = write_sector(cxt, 0, cxt->firstsector);
2163 if (rc)
2164 goto done;
2165 }
2166
2167 if (cxt->label->nparts_max <= 4 && l->ext_offset) {
2168 /* we have empty extended partition, check if the partition has
2169 * been modified and then cleanup possible remaining EBR */
2170 struct pte *pe = self_pte(cxt, l->ext_index);
2171 unsigned char empty[512] = { 0 };
2172 fdisk_sector_t off = pe ? get_abs_partition_start(pe) : 0;
2173
2174 if (off && pe->changed) {
2175 mbr_set_magic(empty);
2176 write_sector(cxt, off, empty);
2177 }
2178 }
2179
2180 /* EBR (logical partitions) */
2181 for (i = 4; i < cxt->label->nparts_max; i++) {
2182 struct pte *pe = self_pte(cxt, i);
2183
2184 assert(pe);
2185 if (!pe->changed || !pe->offset || !pe->sectorbuffer)
2186 continue;
2187
2188 mbr_set_magic(pe->sectorbuffer);
2189 rc = write_sector(cxt, pe->offset, pe->sectorbuffer);
2190 if (rc)
2191 goto done;
2192 }
2193
2194 done:
2195 return rc;
2196 }
2197
2198 static int dos_locate_disklabel(struct fdisk_context *cxt, int n,
2199 const char **name, uint64_t *offset, size_t *size)
2200 {
2201 assert(cxt);
2202
2203 *name = NULL;
2204 *offset = 0;
2205 *size = 0;
2206
2207 switch (n) {
2208 case 0:
2209 *name = "MBR";
2210 *offset = 0;
2211 *size = 512;
2212 break;
2213 default:
2214 /* extended partitions */
2215 if ((size_t)n - 1 + 4 < cxt->label->nparts_max) {
2216 struct pte *pe = self_pte(cxt, n - 1 + 4);
2217
2218 assert(pe);
2219 assert(pe->private_sectorbuffer);
2220
2221 *name = "EBR";
2222 *offset = (uint64_t) pe->offset * cxt->sector_size;
2223 *size = 512;
2224 } else
2225 return 1;
2226 break;
2227 }
2228
2229 return 0;
2230 }
2231
2232 /*
2233 * Check whether partition entries are ordered by their starting positions.
2234 * Return 0 if OK. Return i if partition i should have been earlier.
2235 * Two separate checks: primary and logical partitions.
2236 */
2237 static int wrong_p_order(struct fdisk_context *cxt, size_t *prev)
2238 {
2239 size_t last_p_start_pos = 0, p_start_pos;
2240 size_t i, last_i = 0;
2241
2242 for (i = 0 ; i < cxt->label->nparts_max; i++) {
2243
2244 struct pte *pe = self_pte(cxt, i);
2245 struct dos_partition *p;
2246
2247 assert(pe);
2248 p = pe->pt_entry;
2249
2250 if (i == 4) {
2251 last_i = 4;
2252 last_p_start_pos = 0;
2253 }
2254 if (is_used_partition(p)) {
2255 p_start_pos = get_abs_partition_start(pe);
2256
2257 if (last_p_start_pos > p_start_pos) {
2258 if (prev)
2259 *prev = last_i;
2260 return i;
2261 }
2262
2263 last_p_start_pos = p_start_pos;
2264 last_i = i;
2265 }
2266 }
2267 return 0;
2268 }
2269
2270 static int dos_get_disklabel_item(struct fdisk_context *cxt, struct fdisk_labelitem *item)
2271 {
2272 int rc = 0;
2273
2274 assert(cxt);
2275 assert(cxt->label);
2276 assert(fdisk_is_label(cxt, DOS));
2277
2278 switch (item->id) {
2279 case FDISK_LABELITEM_ID:
2280 {
2281 unsigned int num = mbr_get_id(cxt->firstsector);
2282 item->name = _("Disk identifier");
2283 item->type = 's';
2284 if (asprintf(&item->data.str, "0x%08x", num) < 0)
2285 rc = -ENOMEM;
2286 break;
2287 }
2288 default:
2289 if (item->id < __FDISK_NLABELITEMS)
2290 rc = 1; /* unsupported generic item */
2291 else
2292 rc = 2; /* out of range */
2293 break;
2294 }
2295
2296 return rc;
2297
2298 }
2299
2300 static int dos_get_partition(struct fdisk_context *cxt, size_t n,
2301 struct fdisk_partition *pa)
2302 {
2303 struct dos_partition *p;
2304 struct pte *pe;
2305 struct fdisk_dos_label *lb;
2306
2307 assert(cxt);
2308 assert(pa);
2309 assert(cxt->label);
2310 assert(fdisk_is_label(cxt, DOS));
2311
2312 lb = self_label(cxt);
2313
2314 pe = self_pte(cxt, n);
2315 assert(pe);
2316
2317 p = pe->pt_entry;
2318 pa->used = !is_cleared_partition(p);
2319 if (!pa->used)
2320 return 0;
2321
2322 pa->type = dos_partition_parttype(cxt, p);
2323 pa->boot = p->boot_ind == ACTIVE_FLAG ? 1 : 0;
2324 pa->start = get_abs_partition_start(pe);
2325 pa->size = dos_partition_get_size(p);
2326 pa->container = lb->ext_offset && n == lb->ext_index;
2327
2328 if (n >= 4)
2329 pa->parent_partno = lb->ext_index;
2330
2331 if (p->boot_ind && asprintf(&pa->attrs, "%02x", p->boot_ind) < 0)
2332 return -ENOMEM;
2333
2334 /* start C/H/S */
2335 if (asprintf(&pa->start_chs, "%d/%d/%d",
2336 cylinder(p->bs, p->bc),
2337 p->bh,
2338 sector(p->bs)) < 0)
2339 return -ENOMEM;
2340
2341 /* end C/H/S */
2342 if (asprintf(&pa->end_chs, "%d/%d/%d",
2343 cylinder(p->es, p->ec),
2344 p->eh,
2345 sector(p->es)) < 0)
2346 return -ENOMEM;
2347
2348 return 0;
2349 }
2350
2351 static int has_logical(struct fdisk_context *cxt)
2352 {
2353 size_t i;
2354 struct fdisk_dos_label *l = self_label(cxt);
2355
2356 for (i = 4; i < cxt->label->nparts_max; i++) {
2357 if (l->ptes[i].pt_entry)
2358 return 1;
2359 }
2360 return 0;
2361 }
2362
2363 static int dos_set_partition(struct fdisk_context *cxt, size_t n,
2364 struct fdisk_partition *pa)
2365 {
2366 struct fdisk_dos_label *l;
2367 struct dos_partition *p;
2368 struct pte *pe;
2369 int orgtype;
2370 fdisk_sector_t start, size;
2371
2372 assert(cxt);
2373 assert(pa);
2374 assert(cxt->label);
2375 assert(fdisk_is_label(cxt, DOS));
2376
2377 if (n >= cxt->label->nparts_max)
2378 return -EINVAL;
2379
2380 l = self_label(cxt);
2381 p = self_partition(cxt, n);
2382 assert(p);
2383
2384 pe = self_pte(cxt, n);
2385 if (!pe)
2386 return -EINVAL;
2387
2388 orgtype = p->sys_ind;
2389
2390 if (pa->type) {
2391 if (IS_EXTENDED(pa->type->code) && l->ext_offset && l->ext_index != n) {
2392 fdisk_warnx(cxt, _("Extended partition already exists."));
2393 return -EINVAL;
2394 }
2395
2396 if (!pa->type->code)
2397 fdisk_warnx(cxt, _("Type 0 means free space to many systems. "
2398 "Having partitions of type 0 is probably unwise."));
2399
2400 if (IS_EXTENDED(p->sys_ind) && !IS_EXTENDED(pa->type->code) && has_logical(cxt)) {
2401 fdisk_warnx(cxt, _(
2402 "Cannot change type of the extended partition which is "
2403 "already used by logical partitions. Delete logical "
2404 "partitions first."));
2405 return -EINVAL;
2406 }
2407 }
2408
2409 FDISK_INIT_UNDEF(start);
2410 FDISK_INIT_UNDEF(size);
2411
2412 if (fdisk_partition_has_start(pa))
2413 start = pa->start;
2414 if (fdisk_partition_has_size(pa))
2415 size = pa->size;
2416
2417 if (!FDISK_IS_UNDEF(start) || !FDISK_IS_UNDEF(size)) {
2418 DBG(LABEL, ul_debug("DOS: resize partition"));
2419
2420 if (FDISK_IS_UNDEF(start))
2421 start = get_abs_partition_start(pe);
2422 if (FDISK_IS_UNDEF(size))
2423 size = dos_partition_get_size(p);
2424
2425 set_partition(cxt, n, 0, start, start + size - 1,
2426 pa->type ? pa->type->code : p->sys_ind,
2427 FDISK_IS_UNDEF(pa->boot) ?
2428 p->boot_ind == ACTIVE_FLAG :
2429 fdisk_partition_is_bootable(pa));
2430 } else {
2431 DBG(LABEL, ul_debug("DOS: keep size, modify properties"));
2432 if (pa->type)
2433 p->sys_ind = pa->type->code;
2434 if (!FDISK_IS_UNDEF(pa->boot))
2435 p->boot_ind = fdisk_partition_is_bootable(pa) ? ACTIVE_FLAG : 0;
2436 }
2437
2438 if (pa->type) {
2439 if (IS_EXTENDED(pa->type->code) && !IS_EXTENDED(orgtype)) {
2440 /* new extended partition - create a reference */
2441 l->ext_index = n;
2442 l->ext_offset = dos_partition_get_start(p);
2443 pe->ex_entry = p;
2444 } else if (IS_EXTENDED(orgtype)) {
2445 /* remove extended partition */
2446 cxt->label->nparts_max = 4;
2447 l->ptes[l->ext_index].ex_entry = NULL;
2448 l->ext_offset = 0;
2449 l->ext_index = 0;
2450 }
2451 }
2452
2453 partition_set_changed(cxt, n, 1);
2454 return 0;
2455 }
2456
2457 static void print_chain_of_logicals(struct fdisk_context *cxt)
2458 {
2459 size_t i;
2460 struct fdisk_dos_label *l = self_label(cxt);
2461
2462 fputc('\n', stdout);
2463
2464 for (i = 4; i < cxt->label->nparts_max; i++) {
2465 struct pte *pe = self_pte(cxt, i);
2466
2467 assert(pe);
2468 fprintf(stderr, "#%02zu EBR [%10ju], "
2469 "data[start=%10ju (%10ju), size=%10ju], "
2470 "link[start=%10ju (%10ju), size=%10ju]\n",
2471 i, (uintmax_t) pe->offset,
2472 /* data */
2473 (uintmax_t) dos_partition_get_start(pe->pt_entry),
2474 (uintmax_t) get_abs_partition_start(pe),
2475 (uintmax_t) dos_partition_get_size(pe->pt_entry),
2476 /* link */
2477 (uintmax_t) dos_partition_get_start(pe->ex_entry),
2478 (uintmax_t) l->ext_offset + dos_partition_get_start(pe->ex_entry),
2479 (uintmax_t) dos_partition_get_size(pe->ex_entry));
2480 }
2481 }
2482
2483 static int cmp_ebr_offsets(const void *a, const void *b)
2484 {
2485 const struct pte *ae = (const struct pte *) a,
2486 *be = (const struct pte *) b;
2487
2488 if (ae->offset == 0 && be->offset == 0)
2489 return 0;
2490 if (ae->offset == 0)
2491 return 1;
2492 if (be->offset == 0)
2493 return -1;
2494
2495 return cmp_numbers(ae->offset, be->offset);
2496 }
2497
2498 /*
2499 * Fix the chain of logicals.
2500 *
2501 * The function does not modify data partitions within EBR tables
2502 * (pte->pt_entry). It sorts the chain by EBR offsets and then update links
2503 * (pte->ex_entry) between EBR tables.
2504 *
2505 */
2506 static void fix_chain_of_logicals(struct fdisk_context *cxt)
2507 {
2508 struct fdisk_dos_label *l = self_label(cxt);
2509 struct pte *last;
2510 size_t i;
2511
2512 DBG(LABEL, print_chain_of_logicals(cxt));
2513
2514 /* Sort chain by EBR offsets */
2515 qsort(&l->ptes[4], cxt->label->nparts_max - 4, sizeof(struct pte),
2516 cmp_ebr_offsets);
2517
2518 again:
2519 /* Sort data partitions by start */
2520 for (i = 4; i < cxt->label->nparts_max - 1; i++) {
2521 struct pte *cur = self_pte(cxt, i),
2522 *nxt = self_pte(cxt, i + 1);
2523
2524 assert(cur);
2525 assert(nxt);
2526
2527 if (get_abs_partition_start(cur) >
2528 get_abs_partition_start(nxt)) {
2529
2530 struct dos_partition tmp = *cur->pt_entry;
2531 fdisk_sector_t cur_start = get_abs_partition_start(cur),
2532 nxt_start = get_abs_partition_start(nxt);
2533
2534 /* swap data partitions */
2535 *cur->pt_entry = *nxt->pt_entry;
2536 *nxt->pt_entry = tmp;
2537
2538 /* Recount starts according to EBR offsets, the absolute
2539 * address still has to be the same! */
2540 dos_partition_set_start(cur->pt_entry, nxt_start - cur->offset);
2541 dos_partition_sync_chs(cur->pt_entry, cur->offset, cxt->geom.sectors, cxt->geom.heads);
2542 dos_partition_set_start(nxt->pt_entry, cur_start - nxt->offset);
2543 dos_partition_sync_chs(nxt->pt_entry, nxt->offset, cxt->geom.sectors, cxt->geom.heads);
2544
2545 partition_set_changed(cxt, i, 1);
2546 partition_set_changed(cxt, i + 1, 1);
2547 goto again;
2548 }
2549 }
2550
2551 /* Update EBR links */
2552 for (i = 4; i < cxt->label->nparts_max - 1; i++) {
2553 struct pte *cur = self_pte(cxt, i),
2554 *nxt = self_pte(cxt, i + 1);
2555
2556 assert(cur);
2557 assert(nxt);
2558
2559 fdisk_sector_t noff = nxt->offset - l->ext_offset,
2560 ooff = dos_partition_get_start(cur->ex_entry);
2561
2562 if (noff == ooff)
2563 continue;
2564
2565 DBG(LABEL, ul_debug("DOS: fix EBR [%10ju] link %ju -> %ju",
2566 (uintmax_t) cur->offset,
2567 (uintmax_t) ooff, (uintmax_t) noff));
2568
2569 set_partition(cxt, i, 1, nxt->offset,
2570 get_abs_partition_end(nxt),
2571 MBR_DOS_EXTENDED_PARTITION, 0);
2572 }
2573
2574 /* always terminate the chain ! */
2575 last = self_pte(cxt, cxt->label->nparts_max - 1);
2576 if (last) {
2577 clear_partition(last->ex_entry);
2578 partition_set_changed(cxt, cxt->label->nparts_max - 1, 1);
2579 }
2580
2581 DBG(LABEL, print_chain_of_logicals(cxt));
2582 }
2583
2584 static int dos_reorder(struct fdisk_context *cxt)
2585 {
2586 struct pte *pei, *pek;
2587 size_t i,k;
2588
2589 if (!wrong_p_order(cxt, NULL))
2590 return 1;
2591
2592 while ((i = wrong_p_order(cxt, &k)) != 0 && i < 4) {
2593 /* partition i should have come earlier, move it */
2594 /* We have to move data in the MBR */
2595 struct dos_partition *pi, *pk, *pe, pbuf;
2596 pei = self_pte(cxt, i);
2597 pek = self_pte(cxt, k);
2598
2599 assert(pei);
2600 assert(pek);
2601
2602 pe = pei->ex_entry;
2603 pei->ex_entry = pek->ex_entry;
2604 pek->ex_entry = pe;
2605
2606 pi = pei->pt_entry;
2607 pk = pek->pt_entry;
2608
2609 memmove(&pbuf, pi, sizeof(struct dos_partition));
2610 memmove(pi, pk, sizeof(struct dos_partition));
2611 memmove(pk, &pbuf, sizeof(struct dos_partition));
2612
2613 partition_set_changed(cxt, i, 1);
2614 partition_set_changed(cxt, k, 1);
2615 }
2616
2617 if (i)
2618 fix_chain_of_logicals(cxt);
2619
2620 return 0;
2621 }
2622
2623 /**
2624 * fdisk_dos_fix_chs:
2625 * @cxt: fdisk context
2626 *
2627 * Fix beginning and ending C/H/S values for every partition
2628 * according to LBA relative offset, relative beginning and
2629 * size and fdisk idea of disk geometry (sectors per track
2630 * and number of heads).
2631 *
2632 * Returns: number of fixed (changed) partitions.
2633 */
2634 int fdisk_dos_fix_chs(struct fdisk_context *cxt)
2635 {
2636 unsigned int obc, obh, obs; /* old beginning c, h, s */
2637 unsigned int oec, oeh, oes; /* old ending c, h, s */
2638 unsigned int nbc, nbh, nbs; /* new beginning c, h, s */
2639 unsigned int nec, neh, nes; /* new ending c, h, s */
2640 fdisk_sector_t l, sects; /* lba beginning and size */
2641 struct dos_partition *p;
2642 struct pte *pe;
2643 int changed = 0;
2644 size_t i;
2645
2646 assert(fdisk_is_label(cxt, DOS));
2647
2648 for (i = 0; i < cxt->label->nparts_max; i++) {
2649 p = self_partition(cxt, i);
2650 if (!p || !is_used_partition(p))
2651 continue;
2652
2653 pe = self_pte(cxt, i);
2654
2655 /* old beginning c, h, s */
2656 obc = cylinder(p->bs, p->bc);
2657 obh = p->bh;
2658 obs = sector(p->bs);
2659
2660 /* old ending c, h, s */
2661 oec = cylinder(p->es, p->ec);
2662 oeh = p->eh;
2663 oes = sector(p->es);
2664
2665 /* new beginning c, h, s */
2666 l = get_abs_partition_start(pe);
2667 long2chs(cxt, l, &nbc, &nbh, &nbs);
2668 if (l > UINT32_MAX || nbc >= 1024) {
2669 nbc = 1023;
2670 nbh = cxt->geom.heads-1;
2671 nbs = cxt->geom.sectors;
2672 }
2673
2674 /* new ending c, h, s */
2675 sects = dos_partition_get_size(p);
2676 long2chs(cxt, l + sects - 1, &nec, &neh, &nes);
2677 if (lba_overflowed(l, sects) || nec >= 1024) {
2678 nec = 1023;
2679 neh = cxt->geom.heads-1;
2680 nes = cxt->geom.sectors;
2681 }
2682
2683 if (obc != nbc || obh != nbh || obs != nbs ||
2684 oec != nec || oeh != neh || oes != nes) {
2685 DBG(LABEL, ul_debug("DOS: changing %zu partition CHS "
2686 "from (%d, %d, %d)-(%d, %d, %d) "
2687 "to (%d, %d, %d)-(%d, %d, %d)",
2688 i, obc, obh, obs, oec, oeh, oes,
2689 nbc, nbh, nbs, nec, neh, nes));
2690 p->bc = nbc & 0xff;
2691 p->bh = nbh;
2692 p->bs = nbs | ((nbc >> 2) & 0xc0);
2693 p->ec = nec & 0xff;
2694 p->eh = neh;
2695 p->es = nes | ((nec >> 2) & 0xc0);
2696 partition_set_changed(cxt, i, 1);
2697 changed++;
2698 }
2699 }
2700
2701 return changed;
2702 }
2703
2704 /* TODO: use fdisk_set_partition() API */
2705 int fdisk_dos_move_begin(struct fdisk_context *cxt, size_t i)
2706 {
2707 struct pte *pe;
2708 struct dos_partition *p;
2709 unsigned int new, free_start, curr_start, last;
2710 uintmax_t res = 0;
2711 size_t x;
2712 int rc;
2713
2714 assert(cxt);
2715 assert(fdisk_is_label(cxt, DOS));
2716
2717 pe = self_pte(cxt, i);
2718 if (!pe)
2719 return -EINVAL;
2720
2721 p = pe->pt_entry;
2722
2723 if (!is_used_partition(p) || IS_EXTENDED (p->sys_ind)) {
2724 fdisk_warnx(cxt, _("Partition %zu: no data area."), i + 1);
2725 return 0;
2726 }
2727
2728 /* The safe start is at the second sector, but some use-cases require
2729 * to have MBR within the first partition , so default to the first
2730 * sector of the disk or at the second sector of the extended partition
2731 */
2732 free_start = pe->offset ? pe->offset + 1 : 0;
2733
2734 curr_start = get_abs_partition_start(pe);
2735
2736 /* look for a free space before the current start of the partition */
2737 for (x = 0; x < cxt->label->nparts_max; x++) {
2738 unsigned int end;
2739 struct pte *prev_pe = self_pte(cxt, x);
2740 struct dos_partition *prev_p;
2741
2742 assert(prev_pe);
2743
2744 prev_p = prev_pe->pt_entry;
2745 if (!prev_p)
2746 continue;
2747 end = get_abs_partition_start(prev_pe)
2748 + dos_partition_get_size(prev_p);
2749
2750 if (is_used_partition(prev_p) &&
2751 end > free_start && end <= curr_start)
2752 free_start = end;
2753 }
2754
2755 last = get_abs_partition_end(pe);
2756
2757 rc = fdisk_ask_number(cxt, free_start, curr_start, last,
2758 _("New beginning of data"), &res);
2759 if (rc)
2760 return rc;
2761
2762 new = res - pe->offset;
2763
2764 if (new != dos_partition_get_size(p)) {
2765 unsigned int sects = dos_partition_get_size(p)
2766 + dos_partition_get_start(p) - new;
2767
2768 dos_partition_set_size(p, sects);
2769 dos_partition_set_start(p, new);
2770 dos_partition_sync_chs(p, pe->offset, cxt->geom.sectors, cxt->geom.heads);
2771
2772 partition_set_changed(cxt, i, 1);
2773
2774 if (new == 0)
2775 fdisk_info(cxt, _("The new beginning of the partition overlaps the disk "
2776 "label area. Be very careful when using the partition. "
2777 "You can lose all your partitions on the disk."));
2778 }
2779
2780 return rc;
2781 }
2782
2783 static int dos_partition_is_used(
2784 struct fdisk_context *cxt,
2785 size_t i)
2786 {
2787 struct dos_partition *p;
2788
2789 assert(cxt);
2790 assert(cxt->label);
2791 assert(fdisk_is_label(cxt, DOS));
2792
2793 if (i >= cxt->label->nparts_max)
2794 return 0;
2795
2796 p = self_partition(cxt, i);
2797
2798 return p && !is_cleared_partition(p);
2799 }
2800
2801 static int dos_toggle_partition_flag(
2802 struct fdisk_context *cxt,
2803 size_t i,
2804 unsigned long flag)
2805 {
2806 struct dos_partition *p;
2807
2808 assert(cxt);
2809 assert(cxt->label);
2810 assert(fdisk_is_label(cxt, DOS));
2811
2812 if (i >= cxt->label->nparts_max)
2813 return -EINVAL;
2814
2815 p = self_partition(cxt, i);
2816 assert(p);
2817
2818 switch (flag) {
2819 case DOS_FLAG_ACTIVE:
2820 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
2821 fdisk_warnx(cxt, _("Partition %zu: is an extended "
2822 "partition."), i + 1);
2823
2824 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
2825 partition_set_changed(cxt, i, 1);
2826 fdisk_info(cxt, p->boot_ind ?
2827 _("The bootable flag on partition %zu is enabled now.") :
2828 _("The bootable flag on partition %zu is disabled now."),
2829 i + 1);
2830 break;
2831 default:
2832 return 1;
2833 }
2834
2835 return 0;
2836 }
2837
2838 static const struct fdisk_field dos_fields[] =
2839 {
2840 /* basic */
2841 { FDISK_FIELD_DEVICE, N_("Device"), 10, 0 },
2842 { FDISK_FIELD_BOOT, N_("Boot"), 1, 0 },
2843 { FDISK_FIELD_START, N_("Start"), 5, FDISK_FIELDFL_NUMBER },
2844 { FDISK_FIELD_END, N_("End"), 5, FDISK_FIELDFL_NUMBER },
2845 { FDISK_FIELD_SECTORS, N_("Sectors"), 5, FDISK_FIELDFL_NUMBER },
2846 { FDISK_FIELD_CYLINDERS,N_("Cylinders"), 5, FDISK_FIELDFL_NUMBER },
2847 { FDISK_FIELD_SIZE, N_("Size"), 5, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_EYECANDY },
2848 { FDISK_FIELD_TYPEID, N_("Id"), 2, FDISK_FIELDFL_NUMBER },
2849 { FDISK_FIELD_TYPE, N_("Type"), 0.1, 0 },
2850
2851 /* expert mode */
2852 { FDISK_FIELD_SADDR, N_("Start-C/H/S"), 1, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL },
2853 { FDISK_FIELD_EADDR, N_("End-C/H/S"), 1, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL },
2854 { FDISK_FIELD_ATTR, N_("Attrs"), 2, FDISK_FIELDFL_NUMBER | FDISK_FIELDFL_DETAIL }
2855
2856 };
2857
2858 static const struct fdisk_label_operations dos_operations =
2859 {
2860 .probe = dos_probe_label,
2861 .write = dos_write_disklabel,
2862 .verify = dos_verify_disklabel,
2863 .create = dos_create_disklabel,
2864 .locate = dos_locate_disklabel,
2865 .get_item = dos_get_disklabel_item,
2866 .set_id = dos_set_disklabel_id,
2867
2868 .get_part = dos_get_partition,
2869 .set_part = dos_set_partition,
2870 .add_part = dos_add_partition,
2871 .del_part = dos_delete_partition,
2872 .reorder = dos_reorder,
2873
2874 .part_toggle_flag = dos_toggle_partition_flag,
2875 .part_is_used = dos_partition_is_used,
2876
2877 .reset_alignment = dos_reset_alignment,
2878
2879 .deinit = dos_deinit,
2880 };
2881
2882 /*
2883 * allocates DOS in-memory stuff
2884 */
2885 struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt __attribute__ ((__unused__)))
2886 {
2887 struct fdisk_label *lb;
2888 struct fdisk_dos_label *dos;
2889
2890 dos = calloc(1, sizeof(*dos));
2891 if (!dos)
2892 return NULL;
2893
2894 /* initialize generic part of the driver */
2895 lb = (struct fdisk_label *) dos;
2896 lb->name = "dos";
2897 lb->id = FDISK_DISKLABEL_DOS;
2898 lb->op = &dos_operations;
2899
2900 lb->parttypes = dos_parttypes;
2901 lb->nparttypes = ARRAY_SIZE(dos_parttypes) - 1;
2902 lb->parttype_cuts = dos_parttype_cuts;
2903 lb->nparttype_cuts = ARRAY_SIZE(dos_parttype_cuts);
2904
2905 lb->fields = dos_fields;
2906 lb->nfields = ARRAY_SIZE(dos_fields);
2907
2908 lb->geom_min.sectors = 1;
2909 lb->geom_min.heads = 1;
2910 lb->geom_min.cylinders = 1;
2911
2912 lb->geom_max.sectors = 63;
2913 lb->geom_max.heads = 255;
2914 lb->geom_max.cylinders = 1048576;
2915
2916 /* return calloc() result to keep static anaylizers happy */
2917 return (struct fdisk_label *) dos;
2918 }
2919
2920 /**
2921 * fdisk_dos_enable_compatible:
2922 * @lb: DOS label (see fdisk_get_label())
2923 * @enable: 0 or 1
2924 *
2925 * Enables deprecated DOS compatible mode, in this mode library checks for
2926 * cylinders boundary, cases about CHS addressing and another obscure things.
2927 *
2928 * Returns: 0 on success, <0 on error.
2929 */
2930 int fdisk_dos_enable_compatible(struct fdisk_label *lb, int enable)
2931 {
2932 struct fdisk_dos_label *dos = (struct fdisk_dos_label *) lb;
2933
2934 if (!lb)
2935 return -EINVAL;
2936
2937 dos->compatible = enable;
2938 if (enable)
2939 lb->flags |= FDISK_LABEL_FL_REQUIRE_GEOMETRY;
2940 return 0;
2941 }
2942
2943 /**
2944 * fdisk_dos_is_compatible:
2945 * @lb: DOS label
2946 *
2947 * Returns: 0 if DOS compatibility disabled, 1 if enabled
2948 */
2949 int fdisk_dos_is_compatible(struct fdisk_label *lb)
2950 {
2951 return ((struct fdisk_dos_label *) lb)->compatible;
2952 }