File: mac-parts.c

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (224 lines) | stat: -rw-r--r-- 5,146 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 *   Creation Date: <2003/12/04 17:07:05 samuel>
 *   Time-stamp: <2004/01/07 19:36:09 samuel>
 *
 *	<mac-parts.c>
 *
 *	macintosh partition support
 *
 *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2
 *
 */

#include "openbios/config.h"
#include "openbios/bindings.h"
#include "mac-parts.h"
#include "modules.h"

#ifdef CONFIG_DEBUG_MAC_PARTS
#define DPRINTF(fmt, args...) \
do { printk("MAC-PARTS: " fmt , ##args); } while (0)
#else
#define DPRINTF(fmt, args...) do {} while(0)
#endif

typedef struct {
	ullong		offs;
	ullong		size;
	uint		blocksize;
} macparts_info_t;

DECLARE_NODE( macparts, INSTALL_OPEN, sizeof(macparts_info_t), "+/packages/mac-parts" );


#define SEEK( pos )		({ DPUSH(pos); call_parent(seek_xt); POP(); })
#define READ( buf, size )	({ PUSH((ucell)buf); PUSH(size); call_parent(read_xt); POP(); })

/* ( open -- flag ) */
static void
macparts_open( macparts_info_t *di )
{
	char *str = my_args_copy();
	xt_t seek_xt = find_parent_method("seek");
	xt_t read_xt = find_parent_method("read");
	int bs, parnum=0;
	desc_map_t dmap;
	part_entry_t par;
	int ret = 0;
	int want_bootcode = 0;

	DPRINTF("partition %s\n", str);
	if( str ) {
		char *tmp;
		char *comma = strchr(str, ',');
		parnum = atol(str);
		if( *str == 0 || *str == ',' )
			parnum = -1;
		if (comma) {
			tmp = comma + 1;
		} else {
			if (*str >= '0' && *str <= '9') {
				tmp = (char*)"";
			} else {
				tmp = str;
				parnum = -1;
			}
		}
		if (strcmp(tmp, "%BOOT") == 0)
			want_bootcode = 1;
		free(str);
	}

	DPRINTF("want_bootcode %d\n", want_bootcode);
	DPRINTF("macparts_open %d\n", parnum);
	SEEK( 0 );
	if( READ(&dmap, sizeof(dmap)) != sizeof(dmap) )
		goto out;

	/* partition maps might support multiple block sizes; in this case,
	 * pmPyPartStart is typically given in terms of 512 byte blocks.
	 */
	bs = dmap.sbBlockSize;
	if( bs != 512 ) {
		SEEK( 512 );
		READ( &par, sizeof(par) );
		if( par.pmSig == DESC_PART_SIGNATURE )
			bs = 512;
	}
	SEEK( bs );
	if( READ(&par, sizeof(par)) != sizeof(par) )
		goto out;
        if (par.pmSig != DESC_PART_SIGNATURE)
		goto out;

        if (parnum == -1) {
		int firstHFS = -1;
		/* search a bootable partition */
		/* see PowerPC Microprocessor CHRP bindings */

		parnum = 1;
		while (parnum <= par.pmMapBlkCnt) {
			SEEK( (bs * parnum) );
			READ( &par, sizeof(par) );
			if( par.pmSig != DESC_PART_SIGNATURE ||
                            !par.pmPartBlkCnt )
				goto out;

			if (firstHFS == -1 &&
			    strcmp(par.pmPartType, "Apple_HFS") == 0)
				firstHFS = parnum;

			if( (par.pmPartStatus & kPartitionAUXIsBootValid) &&
			    (par.pmPartStatus & kPartitionAUXIsValid) &&
			    (par.pmPartStatus & kPartitionAUXIsAllocated) &&
			    (par.pmPartStatus & kPartitionAUXIsReadable) &&
			    (strcmp(par.pmProcessor, "PowerPC") == 0) ) {
				di->blocksize =(uint)bs;
				di->offs = (llong)par.pmPyPartStart * bs;
				di->size = (llong)par.pmPartBlkCnt * bs;
				if (want_bootcode) {
					di->offs += (llong)par.pmLgBootStart*bs;
					di->size = (llong)par.pmBootSize;
				}
				ret = -1;
				goto out;
			}

			parnum++;
		}
		/* not found */
		if (firstHFS != -1) {
			parnum = firstHFS;
			goto found;
		}
		ret = 0;
		goto out;
        }

	if (parnum == 0) {
		di->blocksize =(uint)bs;
		di->offs = (llong)0;
		di->size = (llong)dmap.sbBlkCount * bs;
		ret = -1;
		goto out;
	}

	if( parnum > par.pmMapBlkCnt)
		goto out;

found:
	SEEK( (bs * parnum) );
	READ( &par, sizeof(par) );
	if( par.pmSig != DESC_PART_SIGNATURE || !par.pmPartBlkCnt )
		goto out;
	if( !(par.pmPartStatus & kPartitionAUXIsValid) ||
	    !(par.pmPartStatus & kPartitionAUXIsAllocated) ||
	    !(par.pmPartStatus & kPartitionAUXIsReadable) )
		goto out;

	ret = -1;
	di->blocksize =(uint)bs;
	di->offs = (llong)par.pmPyPartStart * bs;
	di->size = (llong)par.pmPartBlkCnt * bs;
	if (want_bootcode) {
		di->offs += (llong)par.pmLgBootStart * bs;
		di->size = (llong)par.pmBootSize;
	}

out:
	DPRINTF("offset 0x%llx size 0x%llx\n", di->offs, di->size);
	PUSH( ret);
}

/* ( block0 -- flag? ) */
static void
macparts_probe( macparts_info_t *dummy )
{
	desc_map_t *dmap = (desc_map_t*)POP();

	DPRINTF("macparts_probe %x ?= %x\n", dmap->sbSig, DESC_MAP_SIGNATURE);
	if( dmap->sbSig != DESC_MAP_SIGNATURE )
		RET(0);
	RET(-1);
}

/* ( -- type offset.d size.d ) */
static void
macparts_get_info( macparts_info_t *di )
{
	PUSH( -1 );		/* no type */
	DPUSH( di->offs );
	DPUSH( di->size );
	DPRINTF("macparts_get_info %lld %lld\n", di->offs, di->size);
}

static void
macparts_block_size( macparts_info_t *di )
{
	DPRINTF("macparts_block_size = %x\n", di->blocksize);
	PUSH(di->blocksize);
}

static void
macparts_initialize( macparts_info_t *di )
{
	fword("register-partition-package");
}

NODE_METHODS( macparts ) = {
	{ "probe",	macparts_probe 		},
	{ "open",	macparts_open 		},
	{ "get-info",	macparts_get_info 	},
	{ "block-size",	macparts_block_size 	},
	{ NULL,		macparts_initialize	},
};

void
macparts_init( void )
{
	REGISTER_NODE( macparts );
}