1/*	$OpenBSD: partition_map.h,v 1.42 2016/01/31 23:00:11 krw Exp $	*/
2
3/*
4 * partition_map.h - partition map routines
5 *
6 * Written by Eryk Vershen
7 */
8
9/*
10 * Copyright 1996,1998 by Apple Computer, Inc.
11 *              All Rights Reserved
12 *
13 * Permission to use, copy, modify, and distribute this software and
14 * its documentation for any purpose and without fee is hereby granted,
15 * provided that the above copyright notice appears in all copies and
16 * that both the copyright notice and this permission notice appear in
17 * supporting documentation.
18 *
19 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE.
22 *
23 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
25 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
26 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
27 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 */
29
30#ifndef __partition_map__
31#define __partition_map__
32
33#define	BLOCK0_SIGNATURE	0x4552	/* 'ER' */
34#define	DPME_SIGNATURE		0x504D	/* 'PM' */
35
36#define	DPISTRLEN	32
37
38struct ddmap {
39    uint32_t	ddBlock;	/* 1st driver's starting sbBlkSize block */
40    uint16_t	ddSize;		/* size of 1st driver (512-byte blks) */
41    uint16_t	ddType;		/* system type (1 for Mac+) */
42};
43
44struct entry;
45
46struct partition_map {
47    LIST_HEAD(, entry)	disk_order;
48    LIST_HEAD(, entry)	base_order;
49    char	       *name;
50    int			fd;
51    int			changed;
52    int			blocks_in_map;
53    int			maximum_in_map;
54    unsigned long	media_size;	/* in physical blocks */
55
56    /* On-disk block 0 data. */
57    uint16_t		sbSig;		/* "ER" */
58    uint16_t		sbBlkSize;	/* physical block size of device */
59    uint32_t		sbBlkCount;	/* # of physical blocks on device */
60    uint16_t		sbDevType;	/* device type */
61    uint16_t		sbDevId;	/* device id */
62    uint32_t		sbData;		/* not used */
63    uint16_t		sbDrvrCount;	/* driver descriptor count */
64    struct ddmap	sbDDMap[8];	/* driver descriptor map*/
65    uint8_t		sbReserved[430];
66};
67
68struct entry {
69    LIST_ENTRY(entry)		disk_entry;
70    LIST_ENTRY(entry)		base_entry;
71    struct partition_map       *the_map;
72    long			disk_address;
73
74    /* On-disk dpme block data.*/
75    uint16_t	dpme_signature;		/* "PM" */
76    uint8_t	dpme_reserved_1[2];
77    uint32_t	dpme_map_entries;	/* # of partition entries */
78    uint32_t	dpme_pblock_start;	/* physical block start of partition */
79    uint32_t	dpme_pblocks;		/* physical block count of partition */
80    char	dpme_name[DPISTRLEN+1];	/* name of partition + NUL */
81    char	dpme_type[DPISTRLEN+1];	/* type of partition + NUL */
82    uint32_t	dpme_lblock_start;	/* logical block start of partition */
83    uint32_t	dpme_lblocks;		/* logical block count of partition */
84    uint32_t	dpme_flags;
85#define	DPME_OS_SPECIFIC_1	(1<<8)
86#define	DPME_OS_SPECIFIC_2	(1<<7)
87#define	DPME_OS_PIC_CODE	(1<<6)
88#define	DPME_WRITABLE		(1<<5)
89#define	DPME_READABLE		(1<<4)
90#define	DPME_BOOTABLE		(1<<3)
91#define	DPME_IN_USE		(1<<2)
92#define	DPME_ALLOCATED		(1<<1)
93#define	DPME_VALID		(1<<0)
94    uint32_t	dpme_boot_block;	/* logical block start of boot code */
95    uint32_t	dpme_boot_bytes;	/* byte count of boot code */
96    uint16_t	dpme_load_addr;		/* memory address of boot code */
97    uint8_t	dpme_reserved_2[4];
98    uint32_t	dpme_goto_addr;		/* memory jump address of boot code */
99    uint8_t	dpme_reserved_3[4];
100    uint32_t	dpme_checksum;		/* of the boot code. */
101    char	dpme_processor_id[17];	/* processor type + NUL */
102    uint8_t	dpme_reserved_4[376];
103};
104
105extern const char *kFreeType;
106extern const char *kMapType;
107extern const char *kUnixType;
108extern const char *kHFSType;
109
110extern int lflag;
111extern int rflag;
112
113struct partition_map	*create_partition_map(int, char *, uint64_t, uint32_t);
114struct partition_map	*open_partition_map(int, char *, uint64_t, uint32_t);
115
116struct entry	*find_entry_by_disk_address(long, struct partition_map *);
117struct entry	*find_entry_by_type(const char *, struct partition_map *);
118struct entry	*find_entry_by_base(uint32_t, struct partition_map *);
119
120int	add_partition_to_map(const char *, const char *, uint32_t, uint32_t,
121    struct partition_map *);
122
123int	contains_driver(struct entry *);
124void	free_partition_map(struct partition_map *);
125void	delete_partition_from_map(struct entry *);
126void	move_entry_in_map(long, long, struct partition_map *);
127void	resize_map(long new_size, struct partition_map *);
128void	write_partition_map(struct partition_map *);
129void	dpme_init_flags(struct entry *);
130
131#endif /* __partition_map__ */
132