EMILE/tools/emile_scanbus.c

311 lines
6.5 KiB
C
Raw Normal View History

2004-12-16 23:25:03 +00:00
/*
*
* (c) 2004 Laurent Vivier <Laurent@lvivier.info>
2004-12-16 23:25:03 +00:00
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
2007-10-04 20:55:52 +00:00
#include "libmap.h"
2004-12-16 23:25:03 +00:00
#include "libemile.h"
2007-10-09 19:29:13 +00:00
#include "device.h"
2004-12-16 23:25:03 +00:00
#define EMILE_MAX_DISK 16
#define EMILE_MAX_DEVNAME 16
typedef char device_name_t[EMILE_MAX_DEVNAME];
extern int verbose;
2004-12-21 15:16:11 +00:00
static void print_size(int nb_blocks, int block_size)
{
int B, GB, MB, kB;
B = (nb_blocks * block_size) % 1024;
if (block_size > 1024)
kB = nb_blocks * (block_size / 1024);
else
kB = nb_blocks / (1024 / block_size);
MB = kB / 1024;
kB = kB % 1024;
GB = MB / 1024;
MB = MB % 1024;
if (GB)
printf("%d.%03d GB", GB, MB * 1000 / 1024);
else if (MB)
printf("%d.%03d MB", MB, kB * 1000 / 1024);
else
printf("%d.%03d kB", kB, B * 1000 / 1024);
}
2004-12-22 00:28:24 +00:00
static int emile_scanbus(device_name_t devices[EMILE_MAX_DISK])
2004-12-16 23:25:03 +00:00
{
int i,j;
int fd;
device_name_t dev;
j = 0;
/* scan SCSI disks */
for(i = 0; (i < EMILE_MAX_DISK) && (j < EMILE_MAX_DISK); i++)
2004-12-16 23:25:03 +00:00
{
sprintf(dev, "/dev/sd%c", 'a' + i);
fd = open(dev, O_RDONLY);
if (fd == -1)
break;
2004-12-16 23:25:03 +00:00
close(fd);
strncpy(devices[j++], dev, EMILE_MAX_DEVNAME);
}
/* scan ATA disks: EMILE doesn't manage them, but this
* allows to have all information on all disks
*/
for(i = 0; (i < EMILE_MAX_DISK) && (j < EMILE_MAX_DISK); i++)
{
sprintf(dev, "/dev/hd%c", 'a' + i);
fd = open(dev, O_RDONLY);
if (fd == -1)
continue;
close(fd);
strncpy(devices[j++], dev, EMILE_MAX_DEVNAME);
}
2004-12-16 23:25:03 +00:00
return j;
}
2007-10-09 19:29:13 +00:00
void diskinfo(char* devname)
2004-12-16 23:25:03 +00:00
{
2007-10-04 20:55:52 +00:00
map_t* map;
2004-12-16 23:25:03 +00:00
int j;
int boottype;
2004-12-16 23:25:03 +00:00
char bootblock[BOOTBLOCK_SIZE];
2006-05-29 22:17:46 +00:00
int block_size, block_count;
int ret;
2007-10-09 19:29:13 +00:00
device_io_t device;
2004-12-16 23:25:03 +00:00
2007-10-09 19:29:13 +00:00
printf("%s:", devname);
device_sector_size = 512;
device.data = (void*)device_open(devname, O_RDONLY);
device.write_sector = (stream_read_sector_t)device_write_sector;
device.read_sector = (stream_read_sector_t)device_read_sector;
device.close = (stream_close_t)device_close;
2007-10-10 16:47:03 +00:00
device.get_blocksize = (stream_get_blocksize_t)device_get_blocksize;
2007-10-09 19:29:13 +00:00
map = map_open(&device);
2006-09-13 20:39:55 +00:00
if (map == NULL)
{
2007-10-09 19:29:13 +00:00
fprintf(stderr, "Cannot read map of %s\n", devname);
2006-09-13 20:39:55 +00:00
return;
}
2006-05-29 22:17:46 +00:00
2007-10-04 20:55:52 +00:00
ret = map_geometry(map, &block_size, &block_count);
2006-05-29 22:17:46 +00:00
if ((ret != -1) && verbose)
2004-12-16 23:25:03 +00:00
{
2006-05-29 22:17:46 +00:00
printf(" block size: %d, blocks number: %d (",
block_size, block_count);
print_size(block_count, block_size);
2006-05-29 22:17:46 +00:00
printf(")\n");
2004-12-16 23:25:03 +00:00
}
2006-05-29 22:17:46 +00:00
else putchar('\n');
2006-05-29 22:17:46 +00:00
if (map == NULL)
{
printf("\t<No information available>\n");
return;
}
2007-10-04 20:55:52 +00:00
if (!map_is_valid(map))
2006-05-29 22:17:46 +00:00
{
printf("\t<No valid partition map found>\n");
return;
}
2007-10-04 20:55:52 +00:00
if (map_get_driver_number(map) > 0)
2006-05-29 22:17:46 +00:00
printf(" Drivers\n");
2007-10-04 20:55:52 +00:00
for (j = 0; j < map_get_driver_number(map); j++)
2006-05-29 22:17:46 +00:00
{
int block, size, type, part;
2007-10-04 20:55:52 +00:00
map_get_driver_info(map, j,
2006-05-29 22:17:46 +00:00
&block, &size, &type);
printf(" %d: base: %d size: %d ",
j, block * block_size / 512,
size * block_size / 512);
printf("type: ");
switch(type)
{
case kDriverTypeMacSCSI:
printf("SCSI");
break;
case kDriverTypeMacATA:
printf("ATA");
break;
case kDriverTypeMacSCSIChained:
printf("SCSI chained");
break;
case kDriverTypeMacATAChained:
printf("ATA chained");
break;
default:
printf("unknown (%04x)\n", type);
break;
}
2007-10-04 20:55:52 +00:00
part = map_seek_driver_partition(map,
block * block_size / 512);
2006-05-29 22:17:46 +00:00
if (part == -1)
{
2007-10-04 20:55:52 +00:00
part = map_seek_driver_partition(map,
block);
if (part == -1)
printf(" <invalid>\n");
2007-10-04 20:55:52 +00:00
map_read(map, part);
}
2006-05-29 22:17:46 +00:00
else
2004-12-16 23:25:03 +00:00
{
2007-10-04 20:55:52 +00:00
map_read(map, part);
if (!emile_is_apple_driver(map))
{
2007-10-04 20:55:52 +00:00
part = map_seek_driver_partition(map,
block);
if (part == -1)
printf(" <invalid>\n");
2007-10-04 20:55:52 +00:00
map_read(map, part);
}
2006-05-29 22:17:46 +00:00
}
printf(" <%d: %s [%s], ", part + 1,
2007-10-04 20:55:52 +00:00
map_get_partition_name(map),
map_get_partition_type(map));
switch(map_get_driver_signature(map))
{
case kPatchDriverSignature:
printf("patch driver");
break;
case kSCSIDriverSignature:
printf("SCSI HD driver");
break;
case kATADriverSignature:
printf("ATAPI HD driver");
break;
case kSCSICDDriverSignature:
printf("SCSI CDROM driver");
break;
case kATAPIDriverSignature:
printf("ATAPI CDROM driver");
break;
case kDriveSetupHFSSignature:
printf("Drive Setup HFS partition");
break;
default:
2007-10-04 20:55:52 +00:00
printf("Unknown (0x%08lx)", map_get_driver_signature(map));
break;
}
printf(">\n");
2006-05-29 22:17:46 +00:00
}
printf(" Partitions\n");
2007-10-04 20:55:52 +00:00
for (j = 0; j < map_get_number(map); j++)
2006-05-29 22:17:46 +00:00
{
2007-10-04 20:55:52 +00:00
map_read(map, j);
2006-05-29 22:17:46 +00:00
2007-10-04 20:55:52 +00:00
if (map_partition_is_startup(map))
2006-05-29 22:17:46 +00:00
printf(" --> ");
else
printf(" ");
printf("%d: ", j + 1);
printf("%16s [%-16s] ",
2007-10-04 20:55:52 +00:00
map_get_partition_name(map),
map_get_partition_type(map));
map_bootblock_read(map, bootblock);
boottype = map_bootblock_get_type(bootblock);
2006-05-29 22:17:46 +00:00
switch(boottype)
{
case INVALID_BOOTBLOCK:
break;
case APPLE_BOOTBLOCK:
printf(" <Apple bootblock>");
break;
case EMILE_BOOTBLOCK:
printf(" <EMILE bootblock>");
break;
default:
printf(" <unknown bootblock>");
break;
}
2007-10-04 20:55:52 +00:00
if (map_partition_is_bootable(map))
2006-05-29 22:17:46 +00:00
printf(" *\n");
else
putchar('\n');
if (verbose)
{
int start, count;
int bootstart, bootsize, bootaddr, bootentry;
int checksum;
char processor[16];
2006-09-23 22:09:08 +00:00
if (*(unsigned long*)
2007-10-04 20:55:52 +00:00
(map_get_partition_type(map) + 28))
2006-09-23 22:09:08 +00:00
printf(" patch: %s\n",
2007-10-04 20:55:52 +00:00
map_get_partition_type(map) + 28);
2006-05-29 22:17:46 +00:00
2007-10-04 20:55:52 +00:00
ret = map_get_partition_geometry(map,
2006-05-29 22:17:46 +00:00
&start,
&count);
if( ret != -1)
2004-12-16 23:25:03 +00:00
{
2006-05-29 22:17:46 +00:00
printf(" base:"
" %d, count: %d (",
start, count);
print_size(count, 512);
printf(")\n");
printf(" flags: 0x%08x\n",
2007-10-04 20:55:52 +00:00
map_partition_get_flags(map));
map_get_bootinfo(map, &bootstart,
2006-05-29 22:17:46 +00:00
&bootsize, &bootaddr,
&bootentry, &checksum,
processor);
printf(" "
"Bootstart: %d, Bootsize: %d\n",
bootstart, bootsize);
printf(" "
"Bootaddr: %d, Bootentry: %d\n",
bootaddr, bootentry);
printf(" "
"Checksum: 0x%04x, Processor: %s\n",
checksum, processor);
2004-12-16 23:25:03 +00:00
}
}
}
2007-10-04 20:55:52 +00:00
map_close(map);
2006-05-29 22:17:46 +00:00
}
void scanbus(void)
{
2007-10-09 19:29:13 +00:00
device_name_t devname[EMILE_MAX_DISK];
2006-05-29 22:17:46 +00:00
int count;
int i;
2007-10-09 19:29:13 +00:00
count = emile_scanbus(devname);
2006-05-29 22:17:46 +00:00
if (count == 0)
{
if (errno == EACCES)
{
fprintf(stderr,
"ERROR: cannot access to devices (you should try as root...)\n");
return;
}
printf("No disk found\n");
}
for (i = 0; i < count; i++)
2007-10-09 19:29:13 +00:00
diskinfo(devname[i]);
2004-12-16 23:25:03 +00:00
}