1#!/usr/bin/perl
2
3###############################################################################
4#
5# hfsutils - tools for reading and writing Macintosh HFS volumes
6# Copyright (C) 1996, 1997 Robert Leslie
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21#
22###############################################################################
23
24die "Usage: $0 device-path\n" unless (@ARGV == 1);
25
26($disk) = @ARGV;
27
28format STDOUT_TOP =
29 # Partition Type                HFS Volume Name                Start    Length
30-------------------------------------------------------------------------------
31.
32
33format STDOUT =
34@# @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<< @####### @########
35$bnum, $pmParType, $drVN, $pmPyPartStart, $pmPartBlkCnt
36.
37
38open(DISK, $disk) || die "$disk: $!\n";
39
40$bnum = 1;
41
42do {
43    seek(DISK, 512 * $bnum, 0) || die "seek: $!\n";
44    read(DISK, $block, 512) || die "read: $!\n";
45
46    ($pmSig, $pmMapBlkCnt, $pmPyPartStart, $pmPartBlkCnt, $pmParType) =
47	(unpack('n2 N3 A32 A32 N10 A16', $block))[0, 2..4, 6];
48
49    die "$disk: unsupported partition map\n" if ($pmSig == 0x5453);
50    die "$disk: no partition map\n" unless ($pmSig == 0x504d);
51
52    if ($pmParType eq 'Apple_HFS') {
53	seek(DISK, 512 * ($pmPyPartStart + 2), 0) || die "seek: $!\n";
54	read(DISK, $block, 512) || die "read: $!\n";
55
56	($len, $drVN) = (unpack('n N2 n5 N2 n N n c A27', $block))[13, 14];
57	$drVN = substr($drVN, 0, $len);
58    } else {
59	$drVN = '';
60    }
61
62    write;
63} while ($bnum++ < $pmMapBlkCnt);
64
65close(DISK);
66