2 * Sun (Sparc32/64) partition support
4 * Copyright (C) 2004 Stefan Reinauer
6 * This code is based (and copied in many places) from
7 * mac partition support by Samuel Rydh (samuel@ibrium.se)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
16 #include "libopenbios/bindings.h"
17 #include "libopenbios/load.h"
18 #include "libc/byteorder.h"
19 #include "libc/vsprintf.h"
22 //#define DEBUG_SUN_PARTS
24 #ifdef DEBUG_SUN_PARTS
25 #define DPRINTF(fmt, args...) \
26 do { printk(fmt , ##args); } while (0)
28 #define DPRINTF(fmt, args...)
32 xt_t seek_xt, read_xt;
33 ucell offs_hi, offs_lo;
34 ucell size_hi, size_lo;
36 phandle_t filesystem_ph;
39 DECLARE_NODE( sunparts, INSTALL_OPEN, sizeof(sunparts_info_t), "+/packages/sun-parts" );
41 #define SEEK( pos ) ({ DPUSH(pos); call_parent(di->seek_xt); POP(); })
42 #define READ( buf, size ) ({ PUSH((ucell)buf); PUSH(size); call_parent(di->read_xt); POP(); })
44 /* Layout of SUN partition table */
45 struct sun_disklabel {
46 uint8_t info[128]; /* Informative text string */
52 uint8_t spare[246]; /* Boot information etc. */
53 uint16_t rspeed; /* Disk rotational speed */
54 uint16_t pcylcount; /* Physical cylinder count */
55 uint16_t sparecyl; /* extra sects per cylinder */
56 uint8_t spare2[4]; /* More magic... */
57 uint16_t ilfact; /* Interleave factor */
58 uint16_t ncyl; /* Data cylinder count */
59 uint16_t nacyl; /* Alt. cylinder count */
60 uint16_t ntrks; /* Tracks per cylinder */
61 uint16_t nsect; /* Sectors per track */
62 uint8_t spare3[4]; /* Even more magic... */
63 struct sun_partition {
64 uint32_t start_cylinder;
67 uint16_t magic; /* Magic number */
68 uint16_t csum; /* Label xor'd checksum */
71 /* two helper functions */
74 has_sun_part_magic(unsigned char *sect)
76 struct sun_disklabel *p = (struct sun_disklabel *)sect;
77 uint16_t csum, *ush, tmp16;
79 if (__be16_to_cpu(p->magic) != 0xDABE)
83 for (ush = (uint16_t *)p; ush < (uint16_t *)(p + 1); ush++) {
84 tmp16 = __be16_to_cpu(*ush);
90 /* ( open -- flag ) */
92 sunparts_open( sunparts_info_t *di )
94 char *str = my_args_copy();
98 unsigned char buf[512];
99 struct sun_disklabel *p;
104 DPRINTF("sunparts_open '%s'\n", str );
107 Arguments that we accept:
112 if ( str && strlen(str) ) {
113 /* Detect the arguments */
114 if ((*str >= '0' && *str <= '9') || (*str >= 'a' && *str < ('a' + 8)) || (*str == ',')) {
117 fword("left-parse-string");
118 parstr = pop_fstr_copy();
119 argstr = pop_fstr_copy();
124 /* Convert the id to a partition number */
125 if (parstr && strlen(parstr)) {
126 if (parstr[0] >= 'a' && parstr[0] < ('a' + 8))
127 parnum = parstr[0] - 'a';
129 parnum = atol(parstr);
133 /* Make sure argstr is not null */
137 DPRINTF("parstr: %s argstr: %s parnum: %d\n", parstr, argstr, parnum);
139 di->filesystem_ph = 0;
140 di->read_xt = find_parent_method("read");
141 di->seek_xt = find_parent_method("seek");
144 if (READ(buf, 512) != 512) {
150 if (!has_sun_part_magic(buf)) {
151 DPRINTF("Sun partition magic not found.\n");
157 /* get partition data */
158 p = (struct sun_disklabel *)buf;
160 for (i = 0; i < 8; i++) {
161 DPRINTF("%c: %d + %d, id %x, flags %x\n", 'a' + i,
162 __be32_to_cpu(p->partitions[i].start_cylinder),
163 __be32_to_cpu(p->partitions[i].num_sectors),
164 __be16_to_cpu(p->infos[i].id),
165 __be16_to_cpu(p->infos[i].flags));
171 DPRINTF("Selected partition %d\n", parnum);
173 offs = (long long)__be32_to_cpu(p->partitions[parnum].start_cylinder) *
174 __be16_to_cpu(p->ntrks) * __be16_to_cpu(p->nsect) * bs;
176 di->offs_hi = offs >> BITS;
177 di->offs_lo = offs & (ucell) -1;
178 size = (long long)__be32_to_cpu(p->partitions[parnum].num_sectors) * bs;
180 DPRINTF("Partition size is 0, exiting\n");
184 di->size_hi = size >> BITS;
185 di->size_lo = size & (ucell) -1;
186 di->type = __be16_to_cpu(p->infos[parnum].id);
188 DPRINTF("Found Sun partition, offs %lld size %lld\n",
189 (long long)offs, (long long)size);
191 /* Probe for filesystem at current offset */
192 DPRINTF("sun-parts: about to probe for fs\n");
194 PUSH_ih( my_parent() );
195 parword("find-filesystem");
196 DPRINTF("sun-parts: done fs probe\n");
200 DPRINTF("sun-parts: filesystem found with ph " FMT_ucellx " and args %s\n", ph, argstr);
201 di->filesystem_ph = ph;
203 /* If we have been asked to open a particular file, interpose the filesystem package with
204 the passed filename as an argument */
205 if (argstr && strlen(argstr)) {
211 DPRINTF("sun-parts: no filesystem found; bypassing misc-files interpose\n");
213 /* Solaris Fcode boot blocks assume that the disk-label package will always
214 automatically interpose the "ufs-file-system" package if it exists! We
215 need to mimic this behaviour in order for the boot to work. */
216 push_str("ufs-file-system");
217 feval("find-package");
220 if (argstr && strlen(argstr) && ph) {
232 /* ( block0 -- flag? ) */
234 sunparts_probe( __attribute__((unused))sunparts_info_t *dummy )
236 unsigned char *buf = (unsigned char *)POP();
238 DPRINTF("probing for Sun partitions\n");
240 RET ( has_sun_part_magic(buf) );
243 /* ( -- type offset.d size.d ) */
245 sunparts_get_info( sunparts_info_t *di )
247 DPRINTF("Sun get_info\n");
256 sunparts_block_size( __attribute__((unused))sunparts_info_t *di )
262 sunparts_initialize( __attribute__((unused))sunparts_info_t *di )
264 fword("register-partition-package");
267 /* ( pos.d -- status ) */
269 sunparts_seek(sunparts_info_t *di )
271 long long pos = DPOP();
272 long long offs, size;;
274 DPRINTF("sunparts_seek %llx:\n", pos);
276 /* Seek is invalid if we reach the end of the device */
277 size = ((ducell)di->size_hi << BITS) | di->size_lo;
281 /* Calculate the seek offset for the parent */
282 offs = ((ducell)di->offs_hi << BITS) | di->offs_lo;
286 DPRINTF("sunparts_seek parent offset %llx:\n", offs);
288 call_package(di->seek_xt, my_parent());
291 /* ( buf len -- actlen ) */
293 sunparts_read(sunparts_info_t *di )
295 DPRINTF("sunparts_read\n");
297 /* Pass the read back up to the parent */
298 call_package(di->read_xt, my_parent());
301 /* ( addr -- size ) */
303 sunparts_load( __attribute__((unused))sunparts_info_t *di )
305 /* Invoke the loader */
309 /* ( pathstr len -- ) */
311 sunparts_dir( sunparts_info_t *di )
313 if ( di->filesystem_ph) {
316 PUSH( di->filesystem_ph );
317 fword("find-method");
321 forth_printf("sun-parts: Unable to determine filesystem\n");
327 NODE_METHODS( sunparts ) = {
328 { "probe", sunparts_probe },
329 { "open", sunparts_open },
330 { "get-info", sunparts_get_info },
331 { "block-size", sunparts_block_size },
332 { "seek", sunparts_seek },
333 { "read", sunparts_read },
334 { "load", sunparts_load },
335 { "dir", sunparts_dir },
336 { NULL, sunparts_initialize },
340 sunparts_init( void )
342 REGISTER_NODE( sunparts );