3 * Hans-Joerg Frieden, Hyperion Entertainment
4 * Hans-JoergF@hyperion-entertainment.com
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include "part_amiga.h"
29 #if defined(CONFIG_CMD_IDE) || \
30 defined(CONFIG_CMD_SCSI) || \
31 defined(CONFIG_CMD_USB) || \
32 defined(CONFIG_MMC) || \
33 defined(CONFIG_SYSTEMACE)
38 #define PRINTF(fmt, args...) printf(fmt ,##args)
40 #define PRINTF(fmt, args...)
50 static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
51 static struct rigid_disk_block rdb = {0};
52 static struct bootcode_block bootcode = {0};
55 * Copy a bcpl to a c string
57 static void bcpl_strcpy(char *to, char *from)
70 * Print a BCPL String. BCPL strings start with a byte with the length
71 * of the string, and don't contain a terminating nul character
73 static void bstr_print(char *string)
82 buffer[i++] = *string++;
87 printf("%-10s", buffer);
91 * Sum a block. The checksum of a block must end up at zero
92 * to be valid. The chk_sum field is selected so that adding
95 int sum_block(struct block_header *header)
97 s32 *block = (s32 *)header;
101 for (i = 0; i < header->summed_longs; i++)
108 * Print an AmigaOS disk type. Disk types are a four-byte identifier
109 * describing the file system. They are usually written as a three-letter
110 * word followed by a backslash and a version number. For example,
111 * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
114 static void print_disk_type(u32 disk_type)
117 buffer[0] = (disk_type & 0xFF000000)>>24;
118 buffer[1] = (disk_type & 0x00FF0000)>>16;
119 buffer[2] = (disk_type & 0x0000FF00)>>8;
121 buffer[4] = (disk_type & 0x000000FF) + '0';
123 printf("%s", buffer);
127 * Print the info contained within the given partition block
129 static void print_part_info(struct partition_block *p)
131 struct amiga_part_geometry *g;
133 g = (struct amiga_part_geometry *)&(p->environment);
135 bstr_print(p->drive_name);
137 g->low_cyl * g->block_per_track * g->surfaces ,
138 (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
139 print_disk_type(g->dos_type);
140 printf("\t%5d\n", g->boot_priority);
144 * Search for the Rigid Disk Block. The rigid disk block is required
145 * to be within the first 16 blocks of a drive, needs to have
146 * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
147 * sum-to-zero checksum
149 struct rigid_disk_block *get_rdisk(block_dev_desc_t *dev_desc)
155 s = getenv("amiga_scanlimit");
157 limit = simple_strtoul(s, NULL, 10);
159 limit = AMIGA_BLOCK_LIMIT;
161 for (i=0; i<limit; i++)
163 ulong res = dev_desc->block_read(dev_desc->dev, i, 1,
164 (ulong *)block_buffer);
167 struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
168 if (trdb->id == AMIGA_ID_RDISK)
170 PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
171 if (sum_block((struct block_header *)block_buffer) == 0)
174 memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
175 return (struct rigid_disk_block *)&rdb;
180 PRINTF("Done scanning, no RDB found\n");
185 * Search for boot code
186 * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
190 struct bootcode_block *get_bootcode(block_dev_desc_t *dev_desc)
196 s = getenv("amiga_scanlimit");
198 limit = simple_strtoul(s, NULL, 10);
200 limit = AMIGA_BLOCK_LIMIT;
202 PRINTF("Scanning for BOOT from 0 to %d\n", limit);
204 for (i = 0; i < limit; i++)
206 ulong res = dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)block_buffer);
209 struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
210 if (boot->id == AMIGA_ID_BOOT)
212 PRINTF("BOOT block at %d, checking checksum\n", i);
213 if (sum_block((struct block_header *)block_buffer) == 0)
215 PRINTF("Found valid bootcode block\n");
216 memcpy(&bootcode, boot, sizeof(struct bootcode_block));
223 PRINTF("No boot code found on disk\n");
228 * Test if the given partition has an Amiga partition table/Rigid
231 int test_part_amiga(block_dev_desc_t *dev_desc)
233 struct rigid_disk_block *rdb;
234 struct bootcode_block *bootcode;
236 PRINTF("test_part_amiga: Testing for an Amiga RDB partition\n");
238 rdb = get_rdisk(dev_desc);
241 bootcode = get_bootcode(dev_desc);
243 PRINTF("test_part_amiga: bootable Amiga disk\n");
245 PRINTF("test_part_amiga: non-bootable Amiga disk\n");
251 PRINTF("test_part_amiga: no RDB found\n");
258 * Find partition number partnum on the given drive.
260 static struct partition_block *find_partition(block_dev_desc_t *dev_desc, int partnum)
262 struct rigid_disk_block *rdb;
263 struct partition_block *p;
266 PRINTF("Trying to find partition block %d\n", partnum);
267 rdb = get_rdisk(dev_desc);
270 PRINTF("find_partition: no rdb found\n");
274 PRINTF("find_partition: Scanning partition list\n");
276 block = rdb->partition_list;
277 PRINTF("find_partition: partition list at 0x%x\n", block);
279 while (block != 0xFFFFFFFF)
281 ulong res = dev_desc->block_read(dev_desc->dev, block, 1,
282 (ulong *)block_buffer);
285 p = (struct partition_block *)block_buffer;
286 if (p->id == AMIGA_ID_PART)
288 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
289 if (sum_block((struct block_header *)p) == 0)
291 if (partnum == 0) break;
298 } else block = 0xFFFFFFFF;
299 } else block = 0xFFFFFFFF;
302 if (block == 0xFFFFFFFF)
304 PRINTF("PART block not found\n");
308 return (struct partition_block *)block_buffer;
312 * Get info about a partition
314 int get_partition_info_amiga (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
316 struct partition_block *p = find_partition(dev_desc, part-1);
317 struct amiga_part_geometry *g;
322 g = (struct amiga_part_geometry *)&(p->environment);
323 info->start = g->low_cyl * g->block_per_track * g->surfaces;
324 info->size = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
325 info->blksz = rdb.block_bytes;
326 bcpl_strcpy(info->name, p->drive_name);
329 disk_type = g->dos_type;
331 info->type[0] = (disk_type & 0xFF000000)>>24;
332 info->type[1] = (disk_type & 0x00FF0000)>>16;
333 info->type[2] = (disk_type & 0x0000FF00)>>8;
334 info->type[3] = '\\';
335 info->type[4] = (disk_type & 0x000000FF) + '0';
341 void print_part_amiga (block_dev_desc_t *dev_desc)
343 struct rigid_disk_block *rdb;
344 struct bootcode_block *boot;
345 struct partition_block *p;
349 rdb = get_rdisk(dev_desc);
352 PRINTF("print_part_amiga: no rdb found\n");
356 PRINTF("print_part_amiga: Scanning partition list\n");
358 block = rdb->partition_list;
359 PRINTF("print_part_amiga: partition list at 0x%x\n", block);
361 printf("Summary: DiskBlockSize: %d\n"
363 " Sectors/Track: %d\n"
365 rdb->block_bytes, rdb->cylinders, rdb->sectors,
368 printf(" First Num. \n"
369 "Nr. Part. Name Block Block Type Boot Priority\n");
371 while (block != 0xFFFFFFFF)
375 PRINTF("Trying to load block #0x%X\n", block);
377 res = dev_desc->block_read(dev_desc->dev, block, 1,
378 (ulong *)block_buffer);
381 p = (struct partition_block *)block_buffer;
382 if (p->id == AMIGA_ID_PART)
384 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
385 if (sum_block((struct block_header *)p) == 0)
387 printf("%-4d ", i); i++;
391 } else block = 0xFFFFFFFF;
392 } else block = 0xFFFFFFFF;
395 boot = get_bootcode(dev_desc);
398 printf("Disk is bootable\n");