2N/A/* apple.c - Read macintosh partition tables. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
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/disk.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/partition.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_APPLE_HEADER_MAGIC 0x4552
2N/A#define GRUB_APPLE_PART_MAGIC 0x504D
2N/A
2N/Astruct grub_apple_header
2N/A{
2N/A /* The magic number to identify the partition map, it should have
2N/A the value `0x4552'. */
2N/A grub_uint16_t magic;
2N/A grub_uint16_t blocksize;
2N/A};
2N/A
2N/Astruct grub_apple_part
2N/A{
2N/A /* The magic number to identify this as a partition, it should have
2N/A the value `0x504D'. */
2N/A grub_uint16_t magic;
2N/A
2N/A /* Reserved. */
2N/A grub_uint16_t reserved;
2N/A
2N/A /* The size of the partition map in blocks. */
2N/A grub_uint32_t partmap_size;
2N/A
2N/A /* The first physical block of the partition. */
2N/A grub_uint32_t first_phys_block;
2N/A
2N/A /* The amount of blocks. */
2N/A grub_uint32_t blockcnt;
2N/A
2N/A /* The partition name. */
2N/A char partname[32];
2N/A
2N/A /* The partition type. */
2N/A char parttype[32];
2N/A
2N/A /* The first datablock of the partition. */
2N/A grub_uint32_t datablocks_first;
2N/A
2N/A /* The amount datablocks. */
2N/A grub_uint32_t datablocks_count;
2N/A
2N/A /* The status of the partition. (???) */
2N/A grub_uint32_t status;
2N/A
2N/A /* The first block on which the bootcode can be found. */
2N/A grub_uint32_t bootcode_pos;
2N/A
2N/A /* The size of the bootcode in bytes. */
2N/A grub_uint32_t bootcode_size;
2N/A
2N/A /* The load address of the bootcode. */
2N/A grub_uint32_t bootcode_loadaddr;
2N/A
2N/A /* Reserved. */
2N/A grub_uint32_t reserved2;
2N/A
2N/A /* The entry point of the bootcode. */
2N/A grub_uint32_t bootcode_entrypoint;
2N/A
2N/A /* Reserved. */
2N/A grub_uint32_t reserved3;
2N/A
2N/A /* A checksum of the bootcode. */
2N/A grub_uint32_t bootcode_checksum;
2N/A
2N/A /* The processor type. */
2N/A char processor[16];
2N/A
2N/A /* Padding. */
2N/A grub_uint16_t pad[187];
2N/A};
2N/A
2N/Astatic struct grub_partition_map grub_apple_partition_map;
2N/A
2N/A
2N/Astatic grub_err_t
2N/Aapple_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 part;
2N/A struct grub_apple_header aheader;
2N/A struct grub_apple_part apart;
2N/A int partno = 0, partnum = 0;
2N/A unsigned pos;
2N/A
2N/A part.partmap = &grub_apple_partition_map;
2N/A
2N/A if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
2N/A return grub_errno;
2N/A
2N/A if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
2N/A {
2N/A grub_dprintf ("partition",
2N/A "bad magic (found 0x%x; wanted 0x%x\n",
2N/A grub_be_to_cpu16 (aheader.magic),
2N/A GRUB_APPLE_HEADER_MAGIC);
2N/A goto fail;
2N/A }
2N/A
2N/A pos = grub_be_to_cpu16 (aheader.blocksize);
2N/A
2N/A do
2N/A {
2N/A part.offset = pos / GRUB_DISK_SECTOR_SIZE;
2N/A part.index = pos % GRUB_DISK_SECTOR_SIZE;
2N/A
2N/A if (grub_disk_read (disk, part.offset, part.index,
2N/A sizeof (struct grub_apple_part), &apart))
2N/A return grub_errno;
2N/A
2N/A if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
2N/A {
2N/A grub_dprintf ("partition",
2N/A "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
2N/A partno, grub_be_to_cpu16 (apart.magic),
2N/A GRUB_APPLE_PART_MAGIC);
2N/A break;
2N/A }
2N/A
2N/A if (partnum == 0)
2N/A partnum = grub_be_to_cpu32 (apart.partmap_size);
2N/A
2N/A part.start = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.first_phys_block)
2N/A * grub_be_to_cpu16 (aheader.blocksize))
2N/A / GRUB_DISK_SECTOR_SIZE;
2N/A part.len = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.blockcnt)
2N/A * grub_be_to_cpu16 (aheader.blocksize))
2N/A / GRUB_DISK_SECTOR_SIZE;
2N/A part.offset = pos;
2N/A part.index = partno;
2N/A part.number = partno;
2N/A
2N/A grub_dprintf ("partition",
2N/A "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
2N/A partno, apart.partname, apart.parttype,
2N/A grub_be_to_cpu32 (apart.first_phys_block),
2N/A grub_be_to_cpu32 (apart.blockcnt));
2N/A
2N/A if (hook (disk, &part))
2N/A return grub_errno;
2N/A
2N/A pos += grub_be_to_cpu16 (aheader.blocksize);
2N/A partno++;
2N/A }
2N/A while (partno < partnum);
2N/A
2N/A if (partno != 0)
2N/A return 0;
2N/A
2N/A fail:
2N/A return grub_error (GRUB_ERR_BAD_PART_TABLE,
2N/A "Apple partition map not found");
2N/A}
2N/A
2N/A
2N/A/* Partition map type. */
2N/Astatic struct grub_partition_map grub_apple_partition_map =
2N/A {
2N/A .name = "apple",
2N/A .iterate = apple_partition_map_iterate,
2N/A };
2N/A
2N/AGRUB_MOD_INIT(part_apple)
2N/A{
2N/A grub_partition_map_register (&grub_apple_partition_map);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(part_apple)
2N/A{
2N/A grub_partition_map_unregister (&grub_apple_partition_map);
2N/A}
2N/A