xref: /openbmc/u-boot/disk/part_amiga.c (revision 1e52fea3)
1 /*
2  * (C) Copyright 2001
3  * Hans-Joerg Frieden, Hyperion Entertainment
4  * Hans-JoergF@hyperion-entertainment.com
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 #include <common.h>
25 #include <command.h>
26 #include <ide.h>
27 #include "part_amiga.h"
28 
29 #if defined(CONFIG_CMD_IDE) || \
30     defined(CONFIG_CMD_MG_DISK) || \
31     defined(CONFIG_CMD_SCSI) || \
32     defined(CONFIG_CMD_USB) || \
33     defined(CONFIG_MMC) || \
34     defined(CONFIG_SYSTEMACE)
35 
36 #undef AMIGA_DEBUG
37 
38 #ifdef AMIGA_DEBUG
39 #define PRINTF(fmt, args...) printf(fmt ,##args)
40 #else
41 #define PRINTF(fmt, args...)
42 #endif
43 
44 struct block_header
45 {
46     u32 id;
47     u32 summed_longs;
48     s32 chk_sum;
49 };
50 
51 static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
52 static struct rigid_disk_block rdb = {0};
53 static struct bootcode_block bootcode = {0};
54 
55 /*
56  * Copy a bcpl to a c string
57  */
58 static void bcpl_strcpy(char *to, char *from)
59 {
60     int len = *from++;
61 
62     while (len)
63     {
64 	*to++ = *from++;
65 	len--;
66     }
67     *to = 0;
68 }
69 
70 /*
71  * Print a BCPL String. BCPL strings start with a byte with the length
72  * of the string, and don't contain a terminating nul character
73  */
74 static void bstr_print(char *string)
75 {
76     int len = *string++;
77     char buffer[256];
78     int i;
79 
80     i = 0;
81     while (len)
82     {
83 	buffer[i++] = *string++;
84 	len--;
85     }
86 
87     buffer[i] = 0;
88     printf("%-10s", buffer);
89 }
90 
91 /*
92  * Sum a block. The checksum of a block must end up at zero
93  * to be valid. The chk_sum field is selected so that adding
94  * it yields zero.
95  */
96 int sum_block(struct block_header *header)
97 {
98     s32 *block = (s32 *)header;
99     u32 i;
100     s32 sum = 0;
101 
102     for (i = 0; i < header->summed_longs; i++)
103 	sum += *block++;
104 
105     return (sum != 0);
106 }
107 
108 /*
109  * Print an AmigaOS disk type. Disk types are a four-byte identifier
110  * describing the file system. They are usually written as a three-letter
111  * word followed by a backslash and a version number. For example,
112  * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
113  * DOS\1 is FFS.
114  */
115 static void print_disk_type(u32 disk_type)
116 {
117     char buffer[6];
118     buffer[0] = (disk_type & 0xFF000000)>>24;
119     buffer[1] = (disk_type & 0x00FF0000)>>16;
120     buffer[2] = (disk_type & 0x0000FF00)>>8;
121     buffer[3] = '\\';
122     buffer[4] = (disk_type & 0x000000FF) + '0';
123     buffer[5] = 0;
124     printf("%s", buffer);
125 }
126 
127 /*
128  * Print the info contained within the given partition block
129  */
130 static void print_part_info(struct partition_block *p)
131 {
132     struct amiga_part_geometry *g;
133 
134     g = (struct amiga_part_geometry *)&(p->environment);
135 
136     bstr_print(p->drive_name);
137     printf("%6d\t%6d\t",
138 	   g->low_cyl * g->block_per_track * g->surfaces ,
139 	   (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
140     print_disk_type(g->dos_type);
141     printf("\t%5d\n", g->boot_priority);
142 }
143 
144 /*
145  * Search for the Rigid Disk Block. The rigid disk block is required
146  * to be within the first 16 blocks of a drive, needs to have
147  * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
148  * sum-to-zero checksum
149  */
150 struct rigid_disk_block *get_rdisk(block_dev_desc_t *dev_desc)
151 {
152     int i;
153     int limit;
154     char *s;
155 
156     s = getenv("amiga_scanlimit");
157     if (s)
158 	limit = simple_strtoul(s, NULL, 10);
159     else
160 	limit = AMIGA_BLOCK_LIMIT;
161 
162     for (i=0; i<limit; i++)
163     {
164 	ulong res = dev_desc->block_read(dev_desc->dev, i, 1,
165 					 (ulong *)block_buffer);
166 	if (res == 1)
167 	{
168 	    struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
169 	    if (trdb->id == AMIGA_ID_RDISK)
170 	    {
171 		PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
172 		if (sum_block((struct block_header *)block_buffer) == 0)
173 		{
174 		    PRINTF("FOUND\n");
175 		    memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
176 		    return (struct rigid_disk_block *)&rdb;
177 		}
178 	    }
179 	}
180     }
181     PRINTF("Done scanning, no RDB found\n");
182     return NULL;
183 }
184 
185 /*
186  * Search for boot code
187  * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
188  * Ridgid disk block
189  */
190 
191 struct bootcode_block *get_bootcode(block_dev_desc_t *dev_desc)
192 {
193     int i;
194     int limit;
195     char *s;
196 
197     s = getenv("amiga_scanlimit");
198     if (s)
199 	limit = simple_strtoul(s, NULL, 10);
200     else
201 	limit = AMIGA_BLOCK_LIMIT;
202 
203     PRINTF("Scanning for BOOT from 0 to %d\n", limit);
204 
205     for (i = 0; i < limit; i++)
206     {
207 	ulong res = dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)block_buffer);
208 	if (res == 1)
209 	{
210 	    struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
211 	    if (boot->id == AMIGA_ID_BOOT)
212 	    {
213 		PRINTF("BOOT block at %d, checking checksum\n", i);
214 		if (sum_block((struct block_header *)block_buffer) == 0)
215 		{
216 		    PRINTF("Found valid bootcode block\n");
217 		    memcpy(&bootcode, boot, sizeof(struct bootcode_block));
218 		    return &bootcode;
219 		}
220 	    }
221 	}
222     }
223 
224     PRINTF("No boot code found on disk\n");
225     return 0;
226 }
227 
228 /*
229  * Test if the given partition has an Amiga partition table/Rigid
230  * Disk block
231  */
232 int test_part_amiga(block_dev_desc_t *dev_desc)
233 {
234     struct rigid_disk_block *rdb;
235     struct bootcode_block *bootcode;
236 
237     PRINTF("test_part_amiga: Testing for an Amiga RDB partition\n");
238 
239     rdb = get_rdisk(dev_desc);
240     if (rdb)
241     {
242 	bootcode = get_bootcode(dev_desc);
243 	if (bootcode)
244 	    PRINTF("test_part_amiga: bootable Amiga disk\n");
245 	else
246 	    PRINTF("test_part_amiga: non-bootable Amiga disk\n");
247 
248 	return 0;
249     }
250     else
251     {
252 	PRINTF("test_part_amiga: no RDB found\n");
253 	return -1;
254     }
255 
256 }
257 
258 /*
259  * Find partition number partnum on the given drive.
260  */
261 static struct partition_block *find_partition(block_dev_desc_t *dev_desc, int partnum)
262 {
263     struct rigid_disk_block *rdb;
264     struct partition_block *p;
265     u32 block;
266 
267     PRINTF("Trying to find partition block %d\n", partnum);
268     rdb = get_rdisk(dev_desc);
269     if (!rdb)
270     {
271 	PRINTF("find_partition: no rdb found\n");
272 	return NULL;
273     }
274 
275     PRINTF("find_partition: Scanning partition list\n");
276 
277     block = rdb->partition_list;
278     PRINTF("find_partition: partition list at 0x%x\n", block);
279 
280     while (block != 0xFFFFFFFF)
281     {
282 	ulong res = dev_desc->block_read(dev_desc->dev, block, 1,
283 					 (ulong *)block_buffer);
284 	if (res == 1)
285 	{
286 	    p = (struct partition_block *)block_buffer;
287 	    if (p->id == AMIGA_ID_PART)
288 	    {
289 		PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
290 		if (sum_block((struct block_header *)p) == 0)
291 		{
292 		    if (partnum == 0) break;
293 		    else
294 		    {
295 			partnum--;
296 			block = p->next;
297 		    }
298 		}
299 	    } else block = 0xFFFFFFFF;
300 	} else block = 0xFFFFFFFF;
301     }
302 
303     if (block == 0xFFFFFFFF)
304     {
305 	PRINTF("PART block not found\n");
306 	return NULL;
307     }
308 
309     return (struct partition_block *)block_buffer;
310 }
311 
312 /*
313  * Get info about a partition
314  */
315 int get_partition_info_amiga (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
316 {
317     struct partition_block *p = find_partition(dev_desc, part-1);
318     struct amiga_part_geometry *g;
319     u32 disk_type;
320 
321     if (!p) return -1;
322 
323     g = (struct amiga_part_geometry *)&(p->environment);
324     info->start = g->low_cyl  * g->block_per_track * g->surfaces;
325     info->size  = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
326     info->blksz = rdb.block_bytes;
327     bcpl_strcpy(info->name, p->drive_name);
328 
329 
330     disk_type = g->dos_type;
331 
332     info->type[0] = (disk_type & 0xFF000000)>>24;
333     info->type[1] = (disk_type & 0x00FF0000)>>16;
334     info->type[2] = (disk_type & 0x0000FF00)>>8;
335     info->type[3] = '\\';
336     info->type[4] = (disk_type & 0x000000FF) + '0';
337     info->type[5] = 0;
338 
339     return 0;
340 }
341 
342 void print_part_amiga (block_dev_desc_t *dev_desc)
343 {
344     struct rigid_disk_block *rdb;
345     struct bootcode_block *boot;
346     struct partition_block *p;
347     u32 block;
348     int i = 1;
349 
350     rdb = get_rdisk(dev_desc);
351     if (!rdb)
352     {
353 	PRINTF("print_part_amiga: no rdb found\n");
354 	return;
355     }
356 
357     PRINTF("print_part_amiga: Scanning partition list\n");
358 
359     block = rdb->partition_list;
360     PRINTF("print_part_amiga: partition list at 0x%x\n", block);
361 
362     printf("Summary:  DiskBlockSize: %d\n"
363 	   "          Cylinders    : %d\n"
364 	   "          Sectors/Track: %d\n"
365 	   "          Heads        : %d\n\n",
366 	   rdb->block_bytes, rdb->cylinders, rdb->sectors,
367 	   rdb->heads);
368 
369     printf("                 First   Num. \n"
370 	   "Nr.  Part. Name  Block   Block  Type        Boot Priority\n");
371 
372     while (block != 0xFFFFFFFF)
373     {
374 	ulong res;
375 
376 	PRINTF("Trying to load block #0x%X\n", block);
377 
378 	res = dev_desc->block_read(dev_desc->dev, block, 1,
379 				   (ulong *)block_buffer);
380 	if (res == 1)
381 	{
382 	    p = (struct partition_block *)block_buffer;
383 	    if (p->id == AMIGA_ID_PART)
384 	    {
385 		PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
386 		if (sum_block((struct block_header *)p) == 0)
387 		{
388 		    printf("%-4d ", i); i++;
389 		    print_part_info(p);
390 		    block = p->next;
391 		}
392 	    } else block = 0xFFFFFFFF;
393 	} else block = 0xFFFFFFFF;
394     }
395 
396     boot = get_bootcode(dev_desc);
397     if (boot)
398     {
399 	printf("Disk is bootable\n");
400     }
401 }
402 
403 #endif
404