2N/A/* sun.c - Read SUN style partition tables. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2005,2006,2007 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/partition.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/symbol.h>
2N/A#include <grub/types.h>
2N/A#include <grub/err.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_PARTMAP_SUN_MAGIC 0xDABE
2N/A#define GRUB_PARTMAP_SUN_MAX_PARTS 8
2N/A#define GRUB_PARTMAP_SUN_WHOLE_DISK_ID 0x05
2N/A
2N/Astruct grub_sun_partition_info
2N/A{
2N/A grub_uint8_t spare1;
2N/A grub_uint8_t id;
2N/A grub_uint8_t spare2;
2N/A grub_uint8_t flags;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_sun_partition_descriptor
2N/A{
2N/A grub_uint32_t start_cylinder;
2N/A grub_uint32_t num_sectors;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_sun_block
2N/A{
2N/A grub_uint8_t info[128]; /* Informative text string. */
2N/A grub_uint8_t spare0[14];
2N/A struct grub_sun_partition_info infos[8];
2N/A grub_uint8_t spare1[246]; /* Boot information etc. */
2N/A grub_uint16_t rspeed; /* Disk rotational speed. */
2N/A grub_uint16_t pcylcount; /* Physical cylinder count. */
2N/A grub_uint16_t sparecyl; /* extra sects per cylinder. */
2N/A grub_uint8_t spare2[4]; /* More magic... */
2N/A grub_uint16_t ilfact; /* Interleave factor. */
2N/A grub_uint16_t ncyl; /* Data cylinder count. */
2N/A grub_uint16_t nacyl; /* Alt. cylinder count. */
2N/A grub_uint16_t ntrks; /* Tracks per cylinder. */
2N/A grub_uint16_t nsect; /* Sectors per track. */
2N/A grub_uint8_t spare3[4]; /* Even more magic... */
2N/A struct grub_sun_partition_descriptor partitions[8];
2N/A grub_uint16_t magic; /* Magic number. */
2N/A grub_uint16_t csum; /* Label xor'd checksum. */
2N/A} __attribute__ ((packed));
2N/A
2N/Astatic struct grub_partition_map grub_sun_partition_map;
2N/A
2N/A/* Verify checksum (true=ok). */
2N/Astatic int
2N/Agrub_sun_is_valid (grub_uint16_t *label)
2N/A{
2N/A grub_uint16_t *pos;
2N/A grub_uint16_t sum = 0;
2N/A
2N/A for (pos = label;
2N/A pos < (label + sizeof (struct grub_sun_block) / 2);
2N/A pos++)
2N/A sum ^= *pos;
2N/A
2N/A return ! sum;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Asun_partition_map_iterate (grub_disk_t disk,
2N/A int (*hook) (grub_disk_t disk,
2N/A const grub_partition_t partition))
2N/A{
2N/A struct grub_partition p;
2N/A union
2N/A {
2N/A struct grub_sun_block block_sun;
2N/A grub_uint16_t raw[0];
2N/A } block;
2N/A int partnum;
2N/A grub_err_t err;
2N/A
2N/A p.partmap = &grub_sun_partition_map;
2N/A err = grub_disk_read (disk, 0, 0, sizeof (struct grub_sun_block),
2N/A &block);
2N/A if (err)
2N/A return err;
2N/A
2N/A if (GRUB_PARTMAP_SUN_MAGIC != grub_be_to_cpu16 (block.block_sun.magic))
2N/A return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a sun partition table");
2N/A
2N/A if (! grub_sun_is_valid (block.raw))
2N/A return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
2N/A
2N/A /* Maybe another error value would be better, because partition
2N/A table _is_ recognized but invalid. */
2N/A for (partnum = 0; partnum < GRUB_PARTMAP_SUN_MAX_PARTS; partnum++)
2N/A {
2N/A struct grub_sun_partition_descriptor *desc;
2N/A
2N/A if (block.block_sun.infos[partnum].id == 0
2N/A || block.block_sun.infos[partnum].id == GRUB_PARTMAP_SUN_WHOLE_DISK_ID)
2N/A continue;
2N/A
2N/A desc = &block.block_sun.partitions[partnum];
2N/A p.start = ((grub_uint64_t) grub_be_to_cpu32 (desc->start_cylinder)
2N/A * grub_be_to_cpu16 (block.block_sun.ntrks)
2N/A * grub_be_to_cpu16 (block.block_sun.nsect));
2N/A p.len = grub_be_to_cpu32 (desc->num_sectors);
2N/A p.number = p.index = partnum;
2N/A if (p.len)
2N/A {
2N/A if (hook (disk, &p))
2N/A partnum = GRUB_PARTMAP_SUN_MAX_PARTS;
2N/A }
2N/A }
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A/* Partition map type. */
2N/Astatic struct grub_partition_map grub_sun_partition_map =
2N/A {
2N/A .name = "sun",
2N/A .iterate = sun_partition_map_iterate,
2N/A };
2N/A
2N/AGRUB_MOD_INIT(part_sun)
2N/A{
2N/A grub_partition_map_register (&grub_sun_partition_map);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(part_sun)
2N/A{
2N/A grub_partition_map_unregister (&grub_sun_partition_map);
2N/A}
2N/A