/* $NetBSD: label.c,v 1.51 2024/02/14 13:52:11 martin Exp $ */
/*
* Copyright 1997 Jonathan Stone
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Jonathan Stone.
* 4. The name of Jonathan Stone may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Return 1 if a partition should be ignored when checking
* for overlapping partitions.
*/
static bool
boringpart(const struct disk_part_info *info)
{
if (info->size == 0)
return true;
if (info->flags & PTI_SPECIAL_PARTS)
return true;
return false;
}
/*
* We have some partitions in our "wanted" list that we may not edit,
* like the RAW_PART in disklabel, some that just represent external
* mount entries for the final fstab or similar.
* We have previously sorted pset->parts and pset->infos to be in sync,
* but the former "array" may be shorter.
* Here are a few quick predicates to check for them.
*/
static bool
real_partition(const struct partition_usage_set *pset, int index)
{
if (index < 0 || (size_t)index >= pset->num)
return false;
/*
* Check partitioning for overlapping partitions.
* Returns true if no overlapping partition found.
* Sets reference arguments ovly1 and ovly2 to the indices of
* overlapping partitions if any are found.
*/
static bool
checklabel(struct disk_partitions *parts,
char *ovl1, char *ovl2)
{
part_id i, j;
struct disk_part_info info;
daddr_t istart, iend, jstart, jend;
unsigned int fs_type, fs_sub_type;
if (parts->num_part == 0)
return true;
for (i = 0; i < parts->num_part - 1; i ++ ) {
if (!parts->pscheme->get_part_info(parts, i, &info))
continue;
/* skip unused or reserved partitions */
if (boringpart(&info))
continue;
/*
* check succeeding partitions for overlap.
* O(n^2), but n is small.
*/
istart = info.start;
iend = istart + info.size;
fs_type = info.fs_type;
fs_sub_type = info.fs_sub_type;
for (j = i+1; j < parts->num_part; j++) {
if (!parts->pscheme->get_part_info(parts, j, &info))
continue;
/* skip unused or reserved partitions */
if (boringpart(&info))
continue;
/* get original partition data, in case start moved already */
if (!edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
edit->id, &pinfo))
pinfo = edit->info;
/* ask for new size with old start and current values */
size = getpartsize(edit->pset->parts, pinfo.start,
edit->info.start, edit->info.size);
if (size < 0)
return 0;
if (size > edit->pset->parts->disk_size)
size = edit->pset->parts->disk_size - edit->info.start;
edit->info.size = size;
return 0;
}
static int
set_ffs_opt_pow2(menudesc *m, void *arg)
{
struct single_part_fs_edit *edit = arg;
size_t val = 1 << (edit->offset+m->cursel);
/*
* Trim all leading and trailing whitespace
*/
for (first = NULL, last = NULL, p = buf; *p; p++) {
if (isspace((unsigned char)*p))
continue;
if (first == NULL)
first = p;
last = p;
}
if (last != NULL)
last[1] = 0;
/*
* Most often used file system types, we offer them in a first level menu.
*/
static const uint edit_fs_common_types[] =
{ FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_EFI_SP, FS_BSDLFS, FS_EX2FS };
/*
* Functions for uncommon file system types - we offer the full list,
* but put FFSv2 and FFSv1 at the front and duplicate FS_MSDOS as
* EFI system partition.
*/
static void
init_fs_type_ext(menudesc *menu, void *arg)
{
struct single_part_fs_edit *edit = arg;
uint t = edit->info.fs_type;
size_t i, ndx, max = menu->numopts;
if (t == FS_BSDFFS) {
if (edit->info.fs_sub_type == 3)
menu->cursel = 0;
else if (edit->info.fs_sub_type == 2)
menu->cursel = 1;
else
menu->cursel = 2;
return;
} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
menu->cursel = FSMAXTYPES+2;
return;
}
/* skip the two FFS entries, and do not add FFS later again */
for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
if (i == FS_UNUSED)
continue;
if (i == FS_BSDFFS)
continue;
if (fstypenames[i] == NULL)
continue;
if (i == t) {
menu->cursel = ndx;
break;
}
if (i == FS_MSDOS) {
ndx++;
if (t == FS_EFI_SP) {
menu->cursel = ndx;
break;
}
}
ndx++;
}
}
static int
set_fstype_ext(menudesc *menu, void *arg)
{
struct single_part_fs_edit *edit = arg;
size_t i, ndx, max = menu->numopts;
enum part_type pt;
for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
if (i == FS_UNUSED)
continue;
if (i == FS_BSDFFS)
continue;
if (fstypenames[i] == NULL)
continue;
if (ndx == (size_t)menu->cursel) {
edit->info.fs_type = i;
edit->info.fs_sub_type = 0;
goto found_type;
}
ndx++;
if (i == FS_MSDOS) {
if (ndx == (size_t)menu->cursel) {
edit->info.fs_type = FS_EFI_SP;
edit->info.fs_sub_type = 0;
goto found_type;
}
ndx++;
}
}
return 1;
/*
* Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
* skip later FFS entry in the generic list.
*/
static int
edit_fs_type_ext(menudesc *menu, void *arg)
{
menu_ent *opts;
int m;
size_t i, ndx, cnt;
/*
* Offer a menu selecting the common file system types
*/
static int
edit_fs_type(menudesc *menu, void *arg)
{
struct single_part_fs_edit *edit = arg;
menu_ent *opts;
int m, cnt;
size_t i;
/*
* Shortcut to full menu if we have an exotic value
*/
if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
edit_fs_type_ext(menu, arg);
return 0;
}
for (i = 0; i < __arraycount(edit_fs_common_types); i++)
if (edit->info.fs_type == edit_fs_common_types[i])
break;
if (i >= __arraycount(edit_fs_common_types)) {
edit_fs_type_ext(menu, arg);
return 0;
}
/*
* Starting with a common type, show short menu first
*/
cnt = __arraycount(edit_fs_common_types) + 3;
opts = calloc(cnt, sizeof(*opts));
if (opts == NULL)
return 0;
/* special case entry 0 - 2: three FFS entries */
for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
opts[i+2].opt_name = getfslabelname(edit_fs_common_types[i], 0);
opts[i+2].opt_action = set_fstype;
}
/* duplicate FFS (at offset 2) into first two entries */
opts[0] = opts[1] = opts[2];
opts[0].opt_name = msg_string(MSG_fs_type_ffsv2ea);
opts[1].opt_name = msg_string(MSG_fs_type_ffsv2);
opts[2].opt_name = msg_string(MSG_fs_type_ffs);
/* add secondary sub-menu */
assert(i+2 < (size_t)cnt);
opts[i+2].opt_name = msg_string(MSG_other_fs_type);
opts[i+2].opt_action = edit_fs_type_ext;
/* do we have parts on record already? */
for (i = 0; i < pset->num_write_back; i++)
if (pset->write_back[i] == parts)
return;
/*
* Need to record this partition table for write back
*/
num = pset->num_write_back + 1;
tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
if (!tab)
return;
tab[pset->num_write_back] = parts;
pset->write_back = tab;
pset->num_write_back = num;
}
if (edit.rv == 0) { /* OK, set new data */
edit.info.last_mounted = edit.wanted->mount;
if (is_new_part) {
edit.wanted->parts = pset->parts;
if (!can_newfs_fstype(edit.info.fs_type))
edit.wanted->instflags &= ~PUIINST_NEWFS;
edit.wanted->cur_part_id = pset->parts->pscheme->
add_partition(pset->parts, &edit.info, &err);
if (edit.wanted->cur_part_id == NO_PART)
err_msg_win(err);
else {
pset->parts->pscheme->get_part_info(
pset->parts, edit.wanted->cur_part_id,
&edit.info);
edit.wanted->cur_start = edit.info.start;
edit.wanted->size = edit.info.size;
edit.wanted->type =
edit.info.nat_type->generic_ptype;
edit.wanted->fs_type = edit.info.fs_type;
edit.wanted->fs_version = edit.info.fs_sub_type;
edit.wanted->cur_flags = edit.info.flags;
/* things have changed, re-sort */
renumber_partitions(pset);
}
} else {
if (!pset->parts->pscheme->set_part_info(pset->parts,
edit.id, &edit.info, &err))
err_msg_win(err);
else
pset->cur_free_space += edit.old_info.size -
edit.info.size;
}
/*
* if size has changed, we may need to add or remove
* the option to add partitions
*/
show_partition_adder(menu, pset);
} else if (edit.rv == -1) { /* cancel edit */
if (is_new_part) {
memmove(pset->infos+edit.index,
pset->infos+edit.index+1,
sizeof(*pset->infos)*(pset->num-edit.index));
memmove(menu->opts+edit.index,
menu->opts+edit.index+1,
sizeof(*menu->opts)*(menu->numopts-edit.index));
menu->numopts--;
menu->cursel = 0;
pset->num--;
return -1;
}
pset->infos[edit.index] = edit.old_usage;
} else if (!is_new_part && edit.rv == -2) { /* delete partition */
if (!pset->parts->pscheme->delete_partition(pset->parts,
edit.id, &err)) {
err_msg_win(err);
return 0;
}
remember_deleted(pset,
pset->infos[edit.index].parts);
pset->cur_free_space += edit.info.size;
memmove(pset->infos+edit.index,
pset->infos+edit.index+1,
sizeof(*pset->infos)*(pset->num-edit.index));
memmove(menu->opts+edit.index,
menu->opts+edit.index+1,
sizeof(*menu->opts)*(menu->numopts-edit.index));
menu->numopts--;
menu->cursel = 0;
if (pset->parts->num_part == 0)
menu->cursel = 1; /* skip sentinel line */
/* things have changed, re-sort */
pset->num--;
renumber_partitions(pset);
/* we can likely add new partitions now */
show_partition_adder(menu, pset);
return -1;
}
return 0;
}
static void
update_edit_ptn_menu(menudesc *m, void *arg)
{
struct single_part_fs_edit *edit = arg;
int i;
uint t = edit->info.fs_type;
size_t attr_no;
/* Determine which of the properties can be changed */
for (i = 0; i < m->numopts; i++) {
if (m->opts[i].opt_action == NULL &&
m->opts[i].opt_menu != MENU_mountoptions)
continue;
/* Default to disabled... */
m->opts[i].opt_flags |= OPT_IGNORE;
if ((t == FS_UNUSED || t == FS_SWAP) &&
(m->opts[i].opt_action == edit_fs_preserve ||
m->opts[i].opt_action == edit_fs_mount ||
m->opts[i].opt_action == edit_fs_mountpt ||
m->opts[i].opt_menu == MENU_mountoptions))
continue;
if (m->opts[i].opt_action == edit_install &&
edit->info.nat_type &&
edit->info.nat_type->generic_ptype != PT_root)
/* can only install onto PT_root partitions */
continue;
if (m->opts[i].opt_action == edit_fs_preserve &&
!can_newfs_fstype(t)) {
/* Can not newfs this filesystem */
edit->wanted->instflags &= ~PUIINST_NEWFS;
continue;
}
if (m->opts[i].opt_action == edit_ptn_custom_type) {
attr_no = (size_t)i - edit->first_custom_attr;
if (!edit->pset->parts->pscheme->
custom_attribute_writable(
edit->pset->parts, edit->id, attr_no))
continue;
}
/*
* Do not allow editing of size/start/type when partition
* is defined in some outer partition table already
*/
if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
&& (m->opts[i].opt_action == edit_fs_type
|| m->opts[i].opt_action == edit_fs_start
|| m->opts[i].opt_action == edit_fs_size))
continue;
/* Ok: we want this one */
m->opts[i].opt_flags &= ~OPT_IGNORE;
}
/* Avoid starting at a (now) disabled menu item */
while (m->cursel >= 0 && m->cursel < m->numopts
&& (m->opts[m->cursel].opt_flags & OPT_IGNORE))
m->cursel++;
}
/*
* Some column width depend on translation, we will set these in
* fmt_fspart_header and later use it when formatting single entries
* in fmt_fspart_row.
* The table consist of 3 "size like" columns, all fixed width, then
* ptnheaders_fstype, part_header_col_flag, and finally the mount point
* (which is variable width).
*/
static int fstype_width, flags_width;
static char fspart_separator[MENUSTRSIZE];
static char fspart_title[2*MENUSTRSIZE];
/*
* Format the header of the main partition editor menu.
*/
static void
fmt_fspart_header(menudesc *menu, void *arg)
{
struct partition_usage_set *pset = arg;
char total[6], free_space[6], scol[13], ecol[13], szcol[13],
sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
const char *fstype, *flags;
int i;
size_t ptn;
bool with_clone, with_inst_flag = pset->parts->parent == NULL;
if (!pset->parts->pscheme->get_part_info(pset->parts,
pset->infos[ptn].cur_part_id, &info))
return;
/*
* We use this function in multiple menus, but only want it
* to play with enable/disable in a single one:
*/
if (m->title == fspart_title) {
/*
* Enable / disable this line if it is something
* like RAW_PART
*/
if ((info.flags &
(PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
|| (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
m->opts[ptn].opt_flags |= OPT_IGNORE;
else
m->opts[ptn].opt_flags &= ~OPT_IGNORE;
}
/* create temporary infos for all clones that work out */
clone_cnt = 0;
clones = calloc(selected.num_sel, sizeof(*clones));
if (clones == NULL)
goto err;
off = pset->parts->num_part+1;
memmove(m->opts+off, m->opts+off+1,
(m->numopts-off-1)*sizeof(*m->opts));
m->numopts--;
}
/*
* Called whenever the "add a partition" option may need to be removed
* or added
*/
static void
show_partition_adder(menudesc *m, struct partition_usage_set *pset)
{
if (m->opts == NULL)
return;
/*
* Check a disklabel.
* If there are overlapping active partitions,
* Ask the user if they want to edit the partition or give up.
*/
int
edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
bool install)
{
menu_ent *op;
size_t cnt, i;
bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
bool may_edit_pack =
pset->parts->pscheme->get_disk_pack_name != NULL &&
pset->parts->pscheme->set_disk_pack_name != NULL;
op = pset->menu_opts;
for (i = 0; i < pset->parts->num_part; i++) {
op->opt_action = edit_ptn;
op++;
}
/* separator line between partitions and actions */
op->opt_name = fspart_separator;
op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
op++;
/* followed by new partition adder */
if (may_add) {
op->opt_name = MSG_addpart;
op->opt_flags = OPT_SUB;
op->opt_action = edit_fspart_add;
op++;
}
/* and unit changer */
op->opt_name = MSG_askunits;
op->opt_menu = MENU_sizechoice;
op->opt_flags = OPT_SUB;
op->opt_action = NULL;
op++;
for (;;) {
/* first give the user the option to edit the label... */
pset->ok = true;
process_menu(pset->menu, pset);
if (!pset->ok) {
i = 0;
break;
}
/* User thinks the label is OK. */
i = verify_parts(pset, install);
if (i == 1)
continue;
break;
}
free(pset->menu_opts);
pset->menu_opts = NULL;
free_menu(pset->menu);
pset->menu = -1;
return i != 0;
}
/*
* strip trailing / to avoid confusion in path comparisons later
*/
void
canonicalize_last_mounted(char *path)
{
char *p;
if (path == NULL)
return;
if (strcmp(path, "/") == 0)
return; /* in this case a "trailing" slash is allowed */
for (;;) {
p = strrchr(path, '/');
if (p == NULL)
return;
if (p[1] != 0)
return;
p[0] = 0;
}
}
/*
* Try to get 'last mounted on' (or equiv) from fs superblock.
*/
const char *
get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
uint flags)
{
static char sblk[SBLOCKSIZE] __aligned(8); /* is this enough? */
struct fs *SB = (struct fs *)sblk;
static const off_t sblocks[] = SBLOCKSEARCH;
const off_t *sbp;
const char *mnt = NULL;
int len;
if (fd == -1)
return "";
if (fs_type)
*fs_type = 0;
if (fs_sub_type)
*fs_sub_type = 0;
/*
* If start of partition and allowed by flags check
* for other fs types
*/
if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
/* Probably a FAT filesystem, report volume name */
size_t i;
for (i = 0x51; i >= 0x47; i--) {
if (sblk[i] != ' ')
break;
sblk[i] = 0;
}
sblk[0x52] = 0;
if (fs_type)
*fs_type = FS_MSDOS;
if (fs_sub_type)
*fs_sub_type = sblk[0x53];
return sblk + 0x47;
} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
memcmp(sblk+3, "NTFS ", 8) == 0) {
if (fs_type)
*fs_type = FS_NTFS;
if (fs_sub_type)
*fs_sub_type = MBR_PTYPE_NTFS;
/* XXX dig for volume name attribute ? */
return "";
}
if (!(flags & GLM_LIKELY_FFS))
continue;
/* Maybe we should validate the checksum??? */
switch (SB->fs_magic) {
case FS_UFS1_MAGIC:
case FS_UFS1_MAGIC_SWAPPED:
if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
if (*sbp == SBLOCK_UFS1)
mnt = (const char *)SB->fs_fsmnt;
} else {
/* Check we have the main superblock */
if (SB->fs_sblockloc == *sbp)
mnt = (const char *)SB->fs_fsmnt;
}
if (fs_type)
*fs_type = FS_BSDFFS;
if (fs_sub_type)
*fs_sub_type = 1;
continue;
case FS_UFS2_MAGIC:
case FS_UFS2EA_MAGIC:
case FS_UFS2_MAGIC_SWAPPED:
case FS_UFS2EA_MAGIC_SWAPPED:
/* Check we have the main superblock */
if (SB->fs_sblockloc == *sbp) {
mnt = (const char *)SB->fs_fsmnt;
if (fs_type)
*fs_type = FS_BSDFFS;
if (fs_sub_type)
*fs_sub_type = 2;
}
continue;
}
}
if (mnt == NULL)
return "";
/* If sysinst mounted this last then strip prefix */
len = strlen(targetroot_mnt);
if (memcmp(mnt, targetroot_mnt, len) == 0) {
if (mnt[len] == 0)
return "/";
if (mnt[len] == '/')
return mnt + len;
}
return mnt;
#undef SB
}
/* Ask for a partition offset, check bounds and do the needed roundups */
daddr_t
getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
{
char defstart[24], isize[24], maxpart, minspace, maxspace,
*prompt, *label_msg, valid_parts[4], valid_spaces[4],
space_prompt[1024], *head, *hint_part, *hint_space, *tail;
size_t num_freespace, spaces, ndx;
struct disk_part_free_space *freespace;
daddr_t i, localsizemult, ptn_alignment, min, max;
part_id partn;
struct disk_part_info info;
const char *errmsg = NULL;
min = parts->disk_start;
max = min + parts->disk_size;
/* upper bound on the number of free spaces, plus some slope */
num_freespace = parts->num_part * 2 + 5;
freespace = calloc(num_freespace, sizeof(*freespace));
if (freespace == NULL)
return -1;
localsizemult = sizemult;
errmsg = NULL;
for (;;) {
snprintf(defstart, sizeof defstart, "%" PRIu64,
defpartstart/sizemult);
if (errmsg != NULL && errmsg[0] != 0)
asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
else
prompt = label_msg;
msg_prompt_win(prompt, -1, 13, 70, -1,
(defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
if (label_msg != prompt)
free(prompt);
if (strcmp(defstart, isize) == 0) {
/* Don't do rounding if default accepted */
i = defpartstart;
break;
}
if (isize[1] == '\0' && isize[0] >= 'a' &&
isize[0] <= maxpart) {
partn = isize[0] - 'a';
if (parts->pscheme->get_part_info(parts, partn,
&info)) {
i = info.start + info.size;
localsizemult = 1;
} else {
errmsg = msg_string(MSG_invalid_sector_number);
continue;
}
} else if (isize[1] == '\0' && isize[0] >= minspace &&
isize[0] <= maxspace) {
ndx = isize[0] - minspace;
i = freespace[ndx].start;
localsizemult = 1;
} else if (atoi(isize) == -1) {
i = min;
localsizemult = 1;
} else {
i = parse_disk_pos(isize, &localsizemult,
parts->bytes_per_sector,
parts->pscheme->get_cylinder_size(parts), NULL);
if (i < 0) {
errmsg = msg_string(MSG_invalid_sector_number);
continue;
}
}
/* round to cylinder size if localsizemult != 1 */
int cylsize = parts->pscheme->get_cylinder_size(parts);
i = NUMSEC(i, localsizemult, cylsize);
/* Adjust to start of slice if needed */
if ((i < min && (min - i) < localsizemult) ||
(i > min && (i - min) < localsizemult)) {
i = min;
}
if (max == 0 || i <= max)
break;
errmsg = msg_string(MSG_startoutsidedisk);
}
free(label_msg);
free(freespace);
return i;
}
/* Ask for a partition size, check bounds and do the needed roundups */
daddr_t
getpartsize(struct disk_partitions *parts, daddr_t orig_start,
daddr_t partstart, daddr_t dflt)
{
char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
*label_msg, *prompt, *head, *hint, *tail;
const char *errmsg = NULL;
daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
struct disk_part_info info;
part_id partn;
diskend = parts->disk_start + parts->disk_size;
max = parts->pscheme->max_free_space_at(parts, orig_start);
max += orig_start - partstart;
if (sizemult == 1)
max--; /* with hugher scale proper rounding later will be ok */
/* We need to keep both the unrounded and rounded (_r) max and dflt */
dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
if (max == dflt)
max_r = dflt_r;
else
max_r = max / sizemult;
/* the partition may have been moved and now not fit any longer */
if (dflt > max)
dflt = max;
if (dflt_r > max_r)
dflt_r = max_r;
if (strcmp(isize, dsize) == 0) {
free(label_msg);
return dflt;
}
if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
isize[0] <= maxpartc) {
partn = isize[0] - 'a';
if (parts->pscheme->get_part_info(parts, partn,
&info)) {
i = info.start - partstart -1;
localsizemult = 1;
max_r = max;
}
} else if (atoi(isize) == -1) {
i = max;
localsizemult = 1;
max_r = max;
} else {
i = parse_disk_pos(isize, &localsizemult,
parts->bytes_per_sector,
parts->pscheme->get_cylinder_size(parts), NULL);
if (localsizemult != sizemult)
max_r = max;
}
if (i < 0) {
errmsg = msg_string(MSG_Invalid_numeric);
continue;
} else if (i > max_r) {
errmsg = msg_string(MSG_Too_large);
continue;
}
/*
* partend is aligned to a cylinder if localsizemult
* is not 1 sector
*/
int cylsize = parts->pscheme->get_cylinder_size(parts);
partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
localsizemult, cylsize);
/* Align to end-of-disk or end-of-slice if close enough */
if (partend > (diskend - sizemult)
&& partend < (diskend + sizemult))
partend = diskend;
if (partend > (partstart + max - sizemult)
&& partend < (partstart + max + sizemult))
partend = partstart + max;
/* sanity checks */
if (partend > diskend) {
partend = diskend;
errmsg = msg_string(MSG_endoutsidedisk);
continue;
}
free(label_msg);
if (partend < partstart)
return 0;
return (partend - partstart);
}
/* NOTREACHED */
}
/*
* convert a string to a number of sectors, with a possible unit
* 150M = 150 Megabytes
* 2000c = 2000 cylinders
* 150256s = 150256 sectors
* Without units, use the default (sizemult).
* returns the raw input value, and the unit used. Caller needs to multiply!
* On invalid inputs, returns -1.
*/
daddr_t
parse_disk_pos(
const char *str,
daddr_t *localsizemult,
daddr_t bps,
daddr_t cyl_size,
bool *extend_this)
{
daddr_t val;
char *cp;
bool mult_found;
if (str[0] == '\0') {
return -1;
}
val = strtoull(str, &cp, 10);
mult_found = false;
if (extend_this)
*extend_this = false;
while (*cp != 0) {
if (*cp == 'G' || *cp == 'g') {
if (mult_found)
return -1;
*localsizemult = GIG / bps;
goto next;
}
if (*cp == 'M' || *cp == 'm') {
if (mult_found)
return -1;
*localsizemult = MEG / bps;
goto next;
}
if (*cp == 'c' || *cp == 'C') {
if (mult_found)
return -1;
*localsizemult = cyl_size;
goto next;
}
if (*cp == 's' || *cp == 'S') {
if (mult_found)
return -1;
*localsizemult = 1;
goto next;
}
if (*cp == '+' && extend_this) {
*extend_this = true;
cp++;
break;
}