]>
Commit | Line | Data |
---|---|---|
f937adac | 1 | /* |
0d3e0b37 | 2 | * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved. |
f937adac NS |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it would be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
11 | * | |
12 | * Further, this software is distributed without any warranty that it is | |
13 | * free of the rightful claim of any third person regarding infringement | |
14 | * or the like. Any license provided herein, whether implied or | |
15 | * otherwise, applies only to this software file. Patent licenses, if | |
16 | * any, provided herein do not apply to combinations of this program with | |
17 | * other software, or any other product whatsoever. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | |
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
22 | * | |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, | |
24 | * Mountain View, CA 94043, or: | |
25 | * | |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ | |
31 | */ | |
32 | ||
33 | #include <stdio.h> | |
34 | #include <fcntl.h> | |
35 | #include <unistd.h> | |
36 | #include <sys/types.h> | |
37 | #include <netinet/in.h> | |
38 | #include <dvh.h> | |
39 | #include "pttype.h" | |
40 | ||
41 | #define blksize 512 | |
42 | ||
33a12367 NS |
43 | static u_int32_t |
44 | twos_complement_32bit_sum(u_int32_t *base, int size) | |
f937adac NS |
45 | { |
46 | int i; | |
33a12367 | 47 | u_int32_t sum = 0; |
f937adac | 48 | |
33a12367 | 49 | size = size / sizeof(u_int32_t); |
f937adac NS |
50 | for (i = 0; i < size; i++) |
51 | sum = sum - ntohl(base[i]); | |
52 | return sum; | |
53 | } | |
54 | ||
55 | static int | |
56 | sgi_parttable(char *base) | |
57 | { | |
33a12367 | 58 | u_int32_t csum; |
f937adac NS |
59 | struct volume_header *vh = (struct volume_header *)base; |
60 | ||
61 | if (ntohl(vh->vh_magic) != VHMAGIC) | |
62 | return 0; | |
33a12367 | 63 | csum = twos_complement_32bit_sum((u_int32_t *)vh, |
f937adac NS |
64 | sizeof(struct volume_header)); |
65 | return !csum; | |
66 | } | |
67 | ||
68 | static int | |
69 | dos_parttable(char *base) | |
70 | { | |
71 | return (base[510] == 0x55 && base[511] == 0xaa); | |
72 | } | |
73 | ||
74 | static int | |
75 | aix_parttable(char *base) | |
76 | { | |
77 | return (aixlabel(base)->magic == AIX_LABEL_MAGIC || | |
78 | aixlabel(base)->magic == AIX_LABEL_MAGIC_SWAPPED); | |
79 | } | |
80 | ||
81 | static int | |
82 | sun_parttable(char *base) | |
83 | { | |
84 | unsigned short *ush; | |
85 | int csum = 0; | |
86 | ||
87 | if (sunlabel(base)->magic != SUN_LABEL_MAGIC && | |
88 | sunlabel(base)->magic != SUN_LABEL_MAGIC_SWAPPED) | |
89 | return csum; | |
90 | ush = ((unsigned short *) (sunlabel(base) + 1)) - 1; | |
91 | while (ush >= (unsigned short *)sunlabel(base)) | |
92 | csum ^= *ush--; | |
93 | return !csum; | |
94 | } | |
95 | ||
eae766ca NS |
96 | static int |
97 | mac_parttable(char *base) | |
98 | { | |
99 | return (ntohs(maclabel(base)->magic) == MAC_LABEL_MAGIC || | |
100 | ntohs(maclabel(base)->magic) == MAC_PARTITION_MAGIC || | |
101 | ntohs(maclabel(base)->magic) == MAC_OLD_PARTITION_MAGIC); | |
102 | } | |
103 | ||
f937adac NS |
104 | |
105 | char * | |
106 | pttype(char *device) | |
107 | { | |
108 | int fd; | |
109 | char *type = NULL; | |
110 | char buf[blksize]; | |
111 | ||
112 | if ((fd = open(device, O_RDONLY)) < 0) | |
113 | ; | |
114 | else if (read(fd, buf, blksize) != blksize) | |
115 | ; | |
116 | else { | |
117 | if (sgi_parttable(buf)) | |
118 | type = "SGI"; | |
119 | else if (sun_parttable(buf)) | |
120 | type = "Sun"; | |
121 | else if (aix_parttable(buf)) | |
122 | type = "AIX"; | |
123 | else if (dos_parttable(buf)) | |
124 | type = "DOS"; | |
eae766ca NS |
125 | else if (mac_parttable(buf)) |
126 | type = "Mac"; | |
f937adac NS |
127 | } |
128 | ||
129 | if (fd >= 0) | |
130 | close(fd); | |
131 | return type; | |
132 | } |