/* sunpc.c - Read SUN PC style partition tables. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2005,2006,2007,2009 Free Software Foundation, Inc.
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/partition.h>
#include <grub/disk.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/dl.h>
#include <grub/symbol.h>
#include <grub/types.h>
#include <grub/err.h>
#ifdef GRUB_UTIL
#include <grub/emu/misc.h>
#include <grub/msdos_partition.h>
#endif
GRUB_MOD_LICENSE ("GPLv3+");
#define GRUB_PARTMAP_SUN_PC_MAGIC 0xDABE
#define GRUB_PARTMAP_SUN_PC_MAX_PARTS 16
#define GRUB_PARTMAP_SUN_PC_WHOLE_DISK_ID 0x05
#define GRUB_PARTMAP_SUN_PC_BOOT_ID 0x01
#define GRUB_PARTMAP_SUN_PC_STAGE2_OFFSET 50
struct grub_sun_pc_partition_descriptor
{
grub_uint16_t id;
grub_uint16_t unused;
grub_uint32_t start_sector;
grub_uint32_t num_sectors;
} __attribute__ ((packed));
struct grub_sun_pc_block
{
grub_uint8_t unused[72];
struct grub_sun_pc_partition_descriptor partitions[GRUB_PARTMAP_SUN_PC_MAX_PARTS];
grub_uint8_t unused2[244];
grub_uint16_t magic; /* Magic number. */
grub_uint16_t csum; /* Label xor'd checksum. */
} __attribute__ ((packed));
static struct grub_partition_map grub_sun_pc_partition_map;
/* Verify checksum (true=ok). */
static int
grub_sun_is_valid (grub_uint16_t *label)
{
grub_uint16_t *pos;
grub_uint16_t sum = 0;
for (pos = label;
pos < (label + sizeof (struct grub_sun_pc_block) / 2);
pos++)
sum ^= *pos;
return ! sum;
}
static grub_err_t
sun_pc_partition_map_iterate_extended (grub_disk_t disk,
int (*hook) (grub_disk_t disk, const grub_partition_t partition,
const struct grub_sun_pc_partition_descriptor *sunpc_partition))
{
grub_partition_t p;
union
{
struct grub_sun_pc_block block_sun;
grub_uint16_t raw[0];
} block;
int partnum;
grub_err_t err;
p = (grub_partition_t) grub_zalloc (sizeof (struct grub_partition));
if (! p)
return grub_errno;
p->partmap = &grub_sun_pc_partition_map;
err = grub_disk_read (disk, 1, 0, sizeof (struct grub_sun_pc_block), &block);
if (err)
{
grub_free (p);
return err;
}
if (GRUB_PARTMAP_SUN_PC_MAGIC != grub_le_to_cpu16 (block.block_sun.magic))
{
grub_free (p);
return grub_error (GRUB_ERR_BAD_PART_TABLE,
"not a sun_pc partition table");
}
if (! grub_sun_is_valid (block.raw))
{
grub_free (p);
return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
}
/* Maybe another error value would be better, because partition
table _is_ recognized but invalid. */
for (partnum = 0; partnum < GRUB_PARTMAP_SUN_PC_MAX_PARTS; partnum++)
{
struct grub_sun_pc_partition_descriptor *desc;
#ifdef __sun__
/*
* Don't check for V_UNASSIGNED - There is a bug in Solaris 11 where
* VTOCs will be created with the root slice marked as unassigned.
*/
if (block.block_sun.partitions[partnum].num_sectors == 0 ||
block.block_sun.partitions[partnum].id == GRUB_PARTMAP_SUN_PC_WHOLE_DISK_ID)
#else
if (block.block_sun.partitions[partnum].id == 0
|| block.block_sun.partitions[partnum].id == GRUB_PARTMAP_SUN_PC_WHOLE_DISK_ID)
#endif
continue;
desc = &block.block_sun.partitions[partnum];
p->start = grub_le_to_cpu32 (desc->start_sector);
p->len = grub_le_to_cpu32 (desc->num_sectors);
p->number = partnum;
if (p->len)
{
if (hook (disk, p, desc))
partnum = GRUB_PARTMAP_SUN_PC_MAX_PARTS;
}
}
grub_free (p);
return grub_errno;
}
static grub_err_t
sun_pc_partition_map_iterate (grub_disk_t disk,
int (*hook) (grub_disk_t disk, const grub_partition_t partition))
{
/*
* Wrap generic hooks so we can just call up to
* sun_pc_partition_map_iterate_extended, above
*/
auto int xt_hook (grub_disk_t xtdisk,
const grub_partition_t p,
const struct grub_sun_pc_partition_descriptor *sp);
int xt_hook(grub_disk_t xtdisk,
const grub_partition_t p,
const struct grub_sun_pc_partition_descriptor *sp
__attribute__ ((unused)))
{
return hook(xtdisk, p);
}
return sun_pc_partition_map_iterate_extended(disk, xt_hook);
}
#ifdef GRUB_UTIL
#define MAX_NON_BOOTSLICE_BLOCKS (50 * 1024 * 1024 / 512)
static grub_err_t
sunpc_partition_map_embed (struct grub_disk *disk, unsigned int *nsectors,
grub_embed_type_t embed_type,
grub_disk_addr_t **sectors)
{
grub_disk_addr_t usablestart = GRUB_PARTMAP_SUN_PC_STAGE2_OFFSET, usablelen = 0;
grub_disk_addr_t parentlen = 0;
grub_partition_t bootslicepart = NULL;
grub_partition_t parent, savedpart;
int found = 0, foundbootslice = 0;
int overlap_firstindex = -1;
int overlap_firstindex_ignore = -1;
grub_disk_addr_t partlen_clamped = 0;
unsigned i;
grub_err_t err;
auto int NESTED_FUNC_ATTR find_sunpc_boot_slice (grub_disk_t disk,
const grub_partition_t p,
const struct grub_sun_pc_partition_descriptor *sp);
int NESTED_FUNC_ATTR find_sunpc_boot_slice(
grub_disk_t disk __attribute__ ((unused)),
const grub_partition_t p,
const struct grub_sun_pc_partition_descriptor *sp)
{
grub_disk_addr_t partlen = grub_partition_get_len(p);
if (p->start == 0 && partlen != 0)
{
if (partlen > parentlen)
{
/*
* This partition is invalid since its span is beyond the
* containing msdos partition parent.
*/
grub_util_warn("While looking for a suitable sunpc embedding "
"area, sunpc partition %d (size 0x%llx) is being ignored "
"because it exceeds the size of its containing msdos "
"partition.\n", p->number, (unsigned long long) partlen);
if (overlap_firstindex_ignore == -1)
overlap_firstindex_ignore = p->number;
if (p->number != overlap_firstindex_ignore)
grub_dprintf("sunpc", "overlap partition %d with %d\n",
p->number, overlap_firstindex_ignore);
return 0;
}
if ((partlen < usablestart) ||
(partlen < *nsectors + usablestart))
{
grub_dprintf("sunpc",
"Found a sunpc embedding area in sunpc partition %d"
", but it is too small (size=0x%llx). Ignoring.\n",
p->number, (unsigned long long) partlen);
if (overlap_firstindex_ignore == -1)
overlap_firstindex_ignore = p->number;
if (p->number != overlap_firstindex_ignore)
grub_dprintf("sunpc", "overlap partition %d with %d\n",
p->number, overlap_firstindex_ignore);
return 0;
}
if (p->number != overlap_firstindex_ignore &&
overlap_firstindex_ignore != -1)
grub_dprintf("sunpc", "looking for embedding area, caught "
"overlapping sunpc partitions %d and %d\n",
p->number, overlap_firstindex_ignore);
if (overlap_firstindex == -1)
overlap_firstindex = p->number;
if (p->number != overlap_firstindex)
grub_util_warn("Overlapping sunpc partitions detected (partitions "
"%d and %d). The sunpc partition table is misconfigured "
"and should be repaired as soon as possible to avoid data "
"corruption.\n", p->number, overlap_firstindex);
if (foundbootslice == 1)
return 0;
if (sp->id == GRUB_PARTMAP_SUN_PC_BOOT_ID)
foundbootslice = 1;
else if (found == 1)
return 0;
found = 1;
if (partlen > MAX_NON_BOOTSLICE_BLOCKS)
{
/* Needs revision for sector sizes other than 512 */
partlen_clamped = partlen;
partlen = MAX_NON_BOOTSLICE_BLOCKS;
}
else
partlen_clamped = 0;
usablelen = partlen - GRUB_PARTMAP_SUN_PC_STAGE2_OFFSET;
grub_memcpy (bootslicepart, p, sizeof (*p));
bootslicepart->partmap = &grub_sun_pc_partition_map;
grub_dprintf("sunpc", "Found %s boot slice @ start=0x%llx len=0x%llx"
" id=0x%x\n", (sp->id == GRUB_PARTMAP_SUN_PC_BOOT_ID) ?
"" : "NON", (unsigned long long)p->start,
(unsigned long long) partlen, sp->id);
}
return 0;
}
if (embed_type != GRUB_EMBED_PCBIOS)
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"SUNPC curently supports only PC-BIOS embedding");
if (disk->partition == NULL)
return grub_error (GRUB_ERR_BAD_DEVICE,
"sunpc_partition_map_embed: No disk partition");
/* save the partition */
savedpart = disk->partition;
/* we are called either with sx partition or pX partition */
/* save the parent */
if ((grub_strcmp (disk->partition->partmap->name , "msdos") == 0) &&
(disk->partition->msdostype == GRUB_PC_PARTITION_SUNIXOS ||
disk->partition->msdostype == GRUB_PC_PARTITION_LEGACY_SUNIXOS))
{
/* disk->partition corresponds to a primary or logical partition p[1-N] */
parent = disk->partition;
}
else if (grub_strcmp (disk->partition->partmap->name , "sunpc") == 0)
{
/* disk->partition corresponds to a vtoc slice sX */
parent = disk->partition->parent;
}
else
{
/* this is not our partition XXX what to return here*/
return grub_error (GRUB_ERR_BAD_DEVICE,
"sunpc_partition_map_embed: Not a Solaris partition");
}
bootslicepart = (grub_partition_t) grub_zalloc (sizeof (struct grub_partition));
if (bootslicepart == NULL)
{
grub_util_warn(grub_errmsg);
return grub_error (grub_errno, "can not allocate area for "
"boot slice partition");
}
parentlen = grub_partition_get_len(parent);
disk->partition = parent;
err = sun_pc_partition_map_iterate_extended (disk, find_sunpc_boot_slice);
if (err || found == 0)
{
grub_free(bootslicepart);
disk->partition = savedpart;
if (err)
return err;
return grub_error (GRUB_ERR_BAD_DEVICE,
"Could not find a suitable boot slice,"
" embedding won't be possible!");
}
if (partlen_clamped != 0)
grub_util_warn("The slice containing the embedding area had a length of "
"0x%llx blocks. Only a maximum of 0x%llx blocks from this"
" slice will be used.\n",
(unsigned long long)partlen_clamped,
(unsigned long long)MAX_NON_BOOTSLICE_BLOCKS);
disk->partition = bootslicepart;
disk->partition->parent = parent;
*nsectors = usablelen;
*sectors = grub_zalloc (*nsectors * sizeof (**sectors));
if (!*sectors) {
grub_util_warn(grub_errmsg);
return grub_error(grub_errno, "Failed to allocate area for embed sectors");
}
for (i = 0; i < *nsectors; i++)
(*sectors)[i] = usablestart + i;
return GRUB_ERR_NONE;
}
#endif
/* Partition map type. */
static struct grub_partition_map grub_sun_pc_partition_map =
{
.name = "sunpc",
.iterate = sun_pc_partition_map_iterate,
#ifdef GRUB_UTIL
.embed = sunpc_partition_map_embed
#endif
};
GRUB_MOD_INIT(part_sunpc)
{
grub_partition_map_register (&grub_sun_pc_partition_map);
}
GRUB_MOD_FINI(part_sunpc)
{
grub_partition_map_unregister (&grub_sun_pc_partition_map);
}