Line 0
Link Here
|
|
|
1 |
/* -*- Mode: c; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
2 |
|
3 |
libparted - a library for manipulating disk partitions |
4 |
Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
Contributor: Sven Luther <luther@debian.org> |
21 |
*/ |
22 |
|
23 |
#include "config.h" |
24 |
|
25 |
#include <string.h> |
26 |
|
27 |
#include <parted/parted.h> |
28 |
#include <parted/debug.h> |
29 |
#include <parted/endian.h> |
30 |
|
31 |
#if ENABLE_NLS |
32 |
# include <libintl.h> |
33 |
# define _(String) dgettext (PACKAGE, String) |
34 |
#else |
35 |
# define _(String) (String) |
36 |
#endif /* ENABLE_NLS */ |
37 |
|
38 |
#define DEBUG_BLOCK 0x01 |
39 |
#define DEBUG_CONST 0x02 |
40 |
#define DEBUG_FUNCS 0x04 |
41 |
#define DEBUG_ERROR 0x08 |
42 |
#define DEBUG_READ 0X10 |
43 |
#define DEBUG_WRITE 0X20 |
44 |
#if 0 |
45 |
#define MASTER_DEBUG DEBUG_BLOCK | DEBUG_CONST | DEBUG_FUNCS | DEBUG_ERROR | DEBUG_READ | DEBUG_WRITE |
46 |
#endif |
47 |
|
48 |
#ifdef MASTER_DEBUG |
49 |
#define DPRINT(l,a,b...) do { if ((l) & (MASTER_DEBUG)) fprintf(stderr, "AMIGA : %s: " a, __func__ , ## b); } while (0) |
50 |
#define DPRINTF(l,a,b...) do { if ((l) & (MASTER_DEBUG)) fprintf(stderr, a, ## b); } while (0) |
51 |
#define TRACE() DPRINT(DEBUG_FUNCS, "\n") |
52 |
#else |
53 |
#define DPRINT(l,a,b...) |
54 |
#define DPRINTF(l,a,b...) |
55 |
#define TRACE() |
56 |
#endif /* MASTER_DEBUG */ |
57 |
|
58 |
/* String manipulation */ |
59 |
static void _amiga_set_bstr (const char *cstr, char *bstr, int maxsize) { |
60 |
int size = strlen (cstr); |
61 |
int i; |
62 |
TRACE(); |
63 |
|
64 |
if (size >= maxsize) return; |
65 |
bstr[0] = size; |
66 |
for (i = 0; i<size; i++) bstr[i+1] = cstr[i]; |
67 |
} |
68 |
static const char * _amiga_get_bstr (char * bstr) { |
69 |
char * cstr = bstr + 1; |
70 |
int size = bstr[0]; |
71 |
TRACE(); |
72 |
|
73 |
cstr[size] = '\0'; |
74 |
return cstr; |
75 |
} |
76 |
|
77 |
#define IDNAME_RIGIDDISK 0x5244534B /* 'RDSK' */ |
78 |
#define IDNAME_BADBLOCK 0x42414442 /* 'BADB' */ |
79 |
#define IDNAME_PARTITION 0x50415254 /* 'PART' */ |
80 |
#define IDNAME_FILESYSHEADER 0x46534844 /* 'FSHD' */ |
81 |
#define IDNAME_LOADSEG 0x4C534547 /* 'LSEG' */ |
82 |
#define IDNAME_BOOT 0x424f4f54 /* 'BOOT' */ |
83 |
#define IDNAME_FREE 0xffffffff |
84 |
|
85 |
static const char * |
86 |
_amiga_block_id (uint32_t id) { |
87 |
TRACE(); |
88 |
switch (id) { |
89 |
case IDNAME_RIGIDDISK : |
90 |
return "RDSK"; |
91 |
case IDNAME_BADBLOCK : |
92 |
return "BADB"; |
93 |
case IDNAME_PARTITION : |
94 |
return "PART"; |
95 |
case IDNAME_FILESYSHEADER : |
96 |
return "FSHD"; |
97 |
case IDNAME_LOADSEG : |
98 |
return "LSEG"; |
99 |
case IDNAME_BOOT : |
100 |
return "BOOT"; |
101 |
case IDNAME_FREE : |
102 |
return "<free>"; |
103 |
default : |
104 |
return "<unknown>"; |
105 |
} |
106 |
} |
107 |
static int |
108 |
_amiga_valid_block_id (uint32_t id) { |
109 |
TRACE(); |
110 |
switch (id) { |
111 |
case IDNAME_RIGIDDISK : |
112 |
case IDNAME_BADBLOCK : |
113 |
case IDNAME_PARTITION : |
114 |
case IDNAME_FILESYSHEADER : |
115 |
case IDNAME_LOADSEG : |
116 |
case IDNAME_BOOT : |
117 |
return 1; |
118 |
case IDNAME_FREE : |
119 |
default : |
120 |
return 0; |
121 |
} |
122 |
} |
123 |
struct AmigaBlock { |
124 |
uint32_t amiga_ID; /* Identifier 32 bit word */ |
125 |
uint32_t amiga_SummedLongss; /* Size of the structure for checksums */ |
126 |
int32_t amiga_ChkSum; /* Checksum of the structure */ |
127 |
}; |
128 |
#define AMIGA(pos) ((struct AmigaBlock *)(pos)) |
129 |
|
130 |
static int |
131 |
_amiga_checksum (struct AmigaBlock *blk) { |
132 |
uint32_t *rdb = (uint32_t *) blk; |
133 |
uint32_t sum; |
134 |
int i, end; |
135 |
TRACE(); |
136 |
|
137 |
sum = PED_BE32_TO_CPU (rdb[0]); |
138 |
end = PED_BE32_TO_CPU (rdb[1]); |
139 |
|
140 |
if (end > PED_SECTOR_SIZE) end = PED_SECTOR_SIZE; |
141 |
|
142 |
for (i = 1; i < end; i++) sum += PED_BE32_TO_CPU (rdb[i]); |
143 |
|
144 |
return sum; |
145 |
} |
146 |
|
147 |
static void |
148 |
_amiga_calculate_checksum (struct AmigaBlock *blk) { |
149 |
TRACE(); |
150 |
blk->amiga_ChkSum = PED_CPU_TO_BE32( |
151 |
PED_BE32_TO_CPU(blk->amiga_ChkSum) - |
152 |
_amiga_checksum((struct AmigaBlock *) blk)); |
153 |
return; |
154 |
} |
155 |
|
156 |
static struct AmigaBlock * |
157 |
_amiga_read_block (PedDevice *dev, struct AmigaBlock *blk, PedSector block) { |
158 |
if (!ped_device_read (dev, blk, block, 1)) { |
159 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
160 |
PED_EXCEPTION_CANCEL, |
161 |
_("%s : Couldn't read block %llu\n"), __func__, block)) |
162 |
{ |
163 |
case PED_EXCEPTION_CANCEL : |
164 |
case PED_EXCEPTION_UNHANDLED : |
165 |
default : |
166 |
return NULL; |
167 |
} |
168 |
} |
169 |
if (_amiga_checksum (blk) != 0) { |
170 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
171 |
PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE | PED_EXCEPTION_CANCEL, |
172 |
_("%s : Bad checksum on block %llu of type %s\n"), |
173 |
__func__, block, _amiga_block_id(PED_BE32_TO_CPU(blk->amiga_ID)))) |
174 |
{ |
175 |
case PED_EXCEPTION_CANCEL : |
176 |
return NULL; |
177 |
case PED_EXCEPTION_FIX : |
178 |
_amiga_calculate_checksum(AMIGA(blk)); |
179 |
if (!ped_device_write (dev, blk, block, 1)) { |
180 |
switch (ped_exception_throw(PED_EXCEPTION_FATAL, |
181 |
PED_EXCEPTION_CANCEL, |
182 |
_("%s : Couldn't write block %d\n"), __func__, block)) |
183 |
{ |
184 |
case PED_EXCEPTION_CANCEL : |
185 |
case PED_EXCEPTION_UNHANDLED : |
186 |
default : |
187 |
return NULL; |
188 |
} |
189 |
} |
190 |
case PED_EXCEPTION_IGNORE : |
191 |
case PED_EXCEPTION_UNHANDLED : |
192 |
default : |
193 |
return blk; |
194 |
} |
195 |
} |
196 |
return blk; |
197 |
} |
198 |
|
199 |
struct RigidDiskBlock { |
200 |
uint32_t rdb_ID; /* Identifier 32 bit word : 'RDSK' */ |
201 |
uint32_t rdb_SummedLongs; /* Size of the structure for checksums */ |
202 |
int32_t rdb_ChkSum; /* Checksum of the structure */ |
203 |
uint32_t rdb_HostID; /* SCSI Target ID of host, not really used */ |
204 |
uint32_t rdb_BlockBytes; /* Size of disk blocks */ |
205 |
uint32_t rdb_Flags; /* RDB Flags */ |
206 |
/* block list heads */ |
207 |
uint32_t rdb_BadBlockList; /* Bad block list */ |
208 |
uint32_t rdb_PartitionList; /* Partition list */ |
209 |
uint32_t rdb_FileSysHeaderList; /* File system header list */ |
210 |
uint32_t rdb_DriveInit; /* Drive specific init code */ |
211 |
uint32_t rdb_BootBlockList; /* Amiga OS 4 Boot Blocks */ |
212 |
uint32_t rdb_Reserved1[5]; /* Unused word, need to be set to $ffffffff */ |
213 |
/* physical drive characteristics */ |
214 |
uint32_t rdb_Cylinders; /* Number of the cylinders of the drive */ |
215 |
uint32_t rdb_Sectors; /* Number of sectors of the drive */ |
216 |
uint32_t rdb_Heads; /* Number of heads of the drive */ |
217 |
uint32_t rdb_Interleave; /* Interleave */ |
218 |
uint32_t rdb_Park; /* Head parking cylinder */ |
219 |
uint32_t rdb_Reserved2[3]; /* Unused word, need to be set to $ffffffff */ |
220 |
uint32_t rdb_WritePreComp; /* Starting cylinder of write precompensation */ |
221 |
uint32_t rdb_ReducedWrite; /* Starting cylinder of reduced write current */ |
222 |
uint32_t rdb_StepRate; /* Step rate of the drive */ |
223 |
uint32_t rdb_Reserved3[5]; /* Unused word, need to be set to $ffffffff */ |
224 |
/* logical drive characteristics */ |
225 |
uint32_t rdb_RDBBlocksLo; /* low block of range reserved for hardblocks */ |
226 |
uint32_t rdb_RDBBlocksHi; /* high block of range for these hardblocks */ |
227 |
uint32_t rdb_LoCylinder; /* low cylinder of partitionable disk area */ |
228 |
uint32_t rdb_HiCylinder; /* high cylinder of partitionable data area */ |
229 |
uint32_t rdb_CylBlocks; /* number of blocks available per cylinder */ |
230 |
uint32_t rdb_AutoParkSeconds; /* zero for no auto park */ |
231 |
uint32_t rdb_HighRDSKBlock; /* highest block used by RDSK */ |
232 |
/* (not including replacement bad blocks) */ |
233 |
uint32_t rdb_Reserved4; |
234 |
/* drive identification */ |
235 |
char rdb_DiskVendor[8]; |
236 |
char rdb_DiskProduct[16]; |
237 |
char rdb_DiskRevision[4]; |
238 |
char rdb_ControllerVendor[8]; |
239 |
char rdb_ControllerProduct[16]; |
240 |
char rdb_ControllerRevision[4]; |
241 |
uint32_t rdb_Reserved5[10]; |
242 |
}; |
243 |
|
244 |
#define RDB_LOCATION_LIMIT 16 |
245 |
#define RDSK(pos) ((struct RigidDiskBlock *)(pos)) |
246 |
|
247 |
/* TODO : print drive ident strings */ |
248 |
void _amiga_dump_rdb (struct RigidDiskBlock * rdb) { |
249 |
TRACE(); |
250 |
DPRINT(DEBUG_BLOCK, "RigidDiskBlock\n\trgb_ID = %08x %s\n", |
251 |
PED_BE32_TO_CPU(rdb->rdb_ID), |
252 |
(PED_BE32_TO_CPU(rdb->rdb_ID)==IDNAME_RIGIDDISK ? "RDSK" : "")); |
253 |
DPRINT(DEBUG_BLOCK, "\trdb_SummedLongs = %08x\n", |
254 |
PED_BE32_TO_CPU(rdb->rdb_SummedLongs)); |
255 |
DPRINT(DEBUG_BLOCK, "\trdb_ChkSum = %08x (block checksum is %d)\n", |
256 |
PED_BE32_TO_CPU(rdb->rdb_ChkSum), _amiga_checksum (AMIGA(rdb))); |
257 |
DPRINT(DEBUG_BLOCK, "\trdb_HostID = %08x\n", PED_BE32_TO_CPU(rdb->rdb_HostID)); |
258 |
DPRINT(DEBUG_BLOCK, "\trdb_BlockBytes = %08x\n", PED_BE32_TO_CPU(rdb->rdb_BlockBytes)); |
259 |
DPRINT(DEBUG_BLOCK, "\trdb_Flags = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Flags)); |
260 |
DPRINT(DEBUG_BLOCK, "\trdb_BadBlockList = %08x\n", PED_BE32_TO_CPU(rdb->rdb_BadBlockList)); |
261 |
DPRINT(DEBUG_BLOCK, "\trdb_PartitionList = %08x\n", PED_BE32_TO_CPU(rdb->rdb_PartitionList)); |
262 |
DPRINT(DEBUG_BLOCK, "\trdb_FileSysHeaderList = %08x\n", PED_BE32_TO_CPU(rdb->rdb_FileSysHeaderList)); |
263 |
DPRINT(DEBUG_BLOCK, "\trdb_DriveInit = %08x\n", PED_BE32_TO_CPU(rdb->rdb_DriveInit)); |
264 |
DPRINT(DEBUG_BLOCK, "\trdb_BootBlockList = %08x\n", PED_BE32_TO_CPU(rdb->rdb_BootBlockList)); |
265 |
DPRINT(DEBUG_BLOCK, "\trdb_Cylinders = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Cylinders)); |
266 |
DPRINT(DEBUG_BLOCK, "\trdb_Sectors = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Sectors)); |
267 |
DPRINT(DEBUG_BLOCK, "\trdb_Heads = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Heads)); |
268 |
DPRINT(DEBUG_BLOCK, "\trdb_Interleave = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Interleave)); |
269 |
DPRINT(DEBUG_BLOCK, "\trdb_Park = %08x\n", PED_BE32_TO_CPU(rdb->rdb_Park)); |
270 |
DPRINT(DEBUG_BLOCK, "\trdb_WritePreComp = %08x\n", PED_BE32_TO_CPU(rdb->rdb_WritePreComp)); |
271 |
DPRINT(DEBUG_BLOCK, "\trdb_ReducedWrite = %08x\n", PED_BE32_TO_CPU(rdb->rdb_ReducedWrite)); |
272 |
DPRINT(DEBUG_BLOCK, "\trdb_StepRate = %08x\n", PED_BE32_TO_CPU(rdb->rdb_StepRate)); |
273 |
DPRINT(DEBUG_BLOCK, "\trdb_RDBBlocksLo = %u\n", PED_BE32_TO_CPU(rdb->rdb_RDBBlocksLo)); |
274 |
DPRINT(DEBUG_BLOCK, "\trdb_RDBBlocksHi = %u\n", PED_BE32_TO_CPU(rdb->rdb_RDBBlocksHi)); |
275 |
DPRINT(DEBUG_BLOCK, "\trdb_LoCylinder = %u\n", PED_BE32_TO_CPU(rdb->rdb_LoCylinder)); |
276 |
DPRINT(DEBUG_BLOCK, "\trdb_HiCylinder = %u\n", PED_BE32_TO_CPU(rdb->rdb_HiCylinder)); |
277 |
DPRINT(DEBUG_BLOCK, "\trdb_CylBlocks = %u (Heads*Sectors = %u\n", |
278 |
PED_BE32_TO_CPU(rdb->rdb_CylBlocks), |
279 |
PED_BE32_TO_CPU(rdb->rdb_Sectors)*PED_BE32_TO_CPU(rdb->rdb_Heads)); |
280 |
DPRINT(DEBUG_BLOCK, "\trdb_AutoParkSeconds = %08x\n", PED_BE32_TO_CPU(rdb->rdb_AutoParkSeconds)); |
281 |
DPRINT(DEBUG_BLOCK, "\trdb_HighRDSKBlock = %08x\n", PED_BE32_TO_CPU(rdb->rdb_HighRDSKBlock)); |
282 |
} |
283 |
|
284 |
#define AMIGA_RDB_NOT_FOUND ((uint32_t)0xffffffff) |
285 |
|
286 |
static uint32_t |
287 |
_amiga_find_rdb (PedDevice *dev, struct RigidDiskBlock *rdb) { |
288 |
int i; |
289 |
TRACE(); |
290 |
|
291 |
for (i = 0; i<RDB_LOCATION_LIMIT; i++) { |
292 |
if (!_amiga_read_block (dev, AMIGA(rdb), i)) { |
293 |
continue; |
294 |
} |
295 |
if (PED_BE32_TO_CPU (rdb->rdb_ID) == IDNAME_RIGIDDISK) { |
296 |
return i; |
297 |
} |
298 |
} |
299 |
return AMIGA_RDB_NOT_FOUND; |
300 |
} |
301 |
|
302 |
struct PartitionBlock { |
303 |
uint32_t pb_ID; /* Identifier 32 bit word : 'PART' */ |
304 |
uint32_t pb_SummedLongs; /* Size of the structure for checksums */ |
305 |
int32_t pb_ChkSum; /* Checksum of the structure */ |
306 |
uint32_t pb_HostID; /* SCSI Target ID of host, not really used */ |
307 |
uint32_t pb_Next; /* Block number of the next PartitionBlock */ |
308 |
uint32_t pb_Flags; /* Part Flags (NOMOUNT and BOOTABLE) */ |
309 |
uint32_t pb_Reserved1[2]; |
310 |
uint32_t pb_DevFlags; /* Preferred flags for OpenDevice */ |
311 |
uint8_t pb_DriveName[32]; /* Preferred DOS device name: BSTR form */ |
312 |
uint32_t pb_Reserved2[15]; |
313 |
uint32_t de_TableSize; /* Size of Environment vector */ |
314 |
uint32_t de_SizeBlock; /* Size of the blocks in 32 bit words, usually 128 */ |
315 |
uint32_t de_SecOrg; /* Not used; must be 0 */ |
316 |
uint32_t de_Surfaces; /* Number of heads (surfaces) */ |
317 |
uint32_t de_SectorPerBlock; /* Disk sectors per block, used with SizeBlock, usually 1 */ |
318 |
uint32_t de_BlocksPerTrack; /* Blocks per track. drive specific */ |
319 |
uint32_t de_Reserved; /* DOS reserved blocks at start of partition. */ |
320 |
uint32_t de_PreAlloc; /* DOS reserved blocks at end of partition */ |
321 |
uint32_t de_Interleave; /* Not used, usually 0 */ |
322 |
uint32_t de_LowCyl; /* First cylinder of the partition */ |
323 |
uint32_t de_HighCyl; /* Last cylinder of the partition */ |
324 |
uint32_t de_NumBuffers; /* Initial # DOS of buffers. */ |
325 |
uint32_t de_BufMemType; /* Type of mem to allocate for buffers */ |
326 |
uint32_t de_MaxTransfer; /* Max number of bytes to transfer at a time */ |
327 |
uint32_t de_Mask; /* Address Mask to block out certain memory */ |
328 |
int32_t de_BootPri; /* Boot priority for autoboot */ |
329 |
uint32_t de_DosType; /* Dostype of the filesystem */ |
330 |
uint32_t de_Baud; /* Baud rate for serial handler */ |
331 |
uint32_t de_Control; /* Control word for handler/filesystem */ |
332 |
uint32_t de_BootBlocks; /* Number of blocks containing boot code */ |
333 |
uint32_t pb_EReserved[12]; |
334 |
}; |
335 |
|
336 |
#define PART(pos) ((struct PartitionBlock *)(pos)) |
337 |
|
338 |
#define PBFB_BOOTABLE 0 /* this partition is intended to be bootable */ |
339 |
#define PBFF_BOOTABLE 1L /* (expected directories and files exist) */ |
340 |
#define PBFB_NOMOUNT 1 /* do not mount this partition (e.g. manually */ |
341 |
#define PBFF_NOMOUNT 2L /* mounted, but space reserved here) */ |
342 |
|
343 |
void _amiga_dump_part (struct PartitionBlock * part) { |
344 |
TRACE(); |
345 |
DPRINT(DEBUG_BLOCK, "Partition : cylinder %d to %d, blocksize : %d (in bytes)\n", |
346 |
PED_BE32_TO_CPU(part->de_LowCyl), |
347 |
PED_BE32_TO_CPU(part->de_HighCyl), |
348 |
PED_BE32_TO_CPU(part->de_SizeBlock)*4*PED_BE32_TO_CPU(part->de_SectorPerBlock)); |
349 |
DPRINT(DEBUG_BLOCK, "\tdostype : %08x, reserved : %d, prealloc : %d\n", |
350 |
PED_BE32_TO_CPU(part->de_DosType), |
351 |
PED_BE32_TO_CPU(part->de_Reserved), |
352 |
PED_BE32_TO_CPU(part->de_PreAlloc)); |
353 |
DPRINT(DEBUG_BLOCK, "PartitionBlock\n"); |
354 |
DPRINT(DEBUG_BLOCK, "\tpart_ID = %08x %s\n", PED_BE32_TO_CPU(part->pb_ID), |
355 |
(PED_BE32_TO_CPU(part->pb_ID)==IDNAME_PARTITION ? "PART" : "")); |
356 |
DPRINT(DEBUG_BLOCK, "\tpart_SummedLongs = %08x\n", |
357 |
PED_BE32_TO_CPU(part->pb_SummedLongs)); |
358 |
DPRINT(DEBUG_BLOCK, "\tpart_ChkSum = %08x (block checksum is %d)\n", |
359 |
PED_BE32_TO_CPU(part->pb_ChkSum), _amiga_checksum (AMIGA(part))); |
360 |
DPRINT(DEBUG_BLOCK, "\tpart_HostID = %08x\n", PED_BE32_TO_CPU(part->pb_HostID)); |
361 |
DPRINT(DEBUG_BLOCK, "\tpart_Next = %08x (%d)\n", |
362 |
PED_BE32_TO_CPU(part->pb_Next), PED_BE32_TO_CPU(part->pb_Next)); |
363 |
DPRINT(DEBUG_BLOCK, "\tpart_Flags = %08x\n", PED_BE32_TO_CPU(part->pb_Flags)); |
364 |
DPRINT(DEBUG_BLOCK, "\tpart_DevFlags = %08x\n", PED_BE32_TO_CPU(part->pb_DevFlags)); |
365 |
DPRINT(DEBUG_BLOCK, "\tpart_DriveName = %s\n", _amiga_get_bstr(part->pb_DriveName)); |
366 |
DPRINT(DEBUG_BLOCK, "\tde_TableSize = %08x\n", PED_BE32_TO_CPU(part->de_TableSize)); |
367 |
DPRINT(DEBUG_BLOCK, "\tde_SizeBlock = %08x\n", PED_BE32_TO_CPU(part->de_SizeBlock)); |
368 |
DPRINT(DEBUG_BLOCK, "\tde_SecOrg = %08x\n", PED_BE32_TO_CPU(part->de_SecOrg)); |
369 |
DPRINT(DEBUG_BLOCK, "\tde_Surfaces = %08x\n", PED_BE32_TO_CPU(part->de_Surfaces)); |
370 |
DPRINT(DEBUG_BLOCK, "\tde_SectorPerBlock = %08x\n", PED_BE32_TO_CPU(part->de_SectorPerBlock)); |
371 |
DPRINT(DEBUG_BLOCK, "\tde_BlocksPerTrack = %08x\n", PED_BE32_TO_CPU(part->de_BlocksPerTrack)); |
372 |
DPRINT(DEBUG_BLOCK, "\tde_Reserved = %08x\n", PED_BE32_TO_CPU(part->de_Reserved)); |
373 |
DPRINT(DEBUG_BLOCK, "\tde_PreAlloc = %08x\n", PED_BE32_TO_CPU(part->de_PreAlloc)); |
374 |
DPRINT(DEBUG_BLOCK, "\tde_Interleave = %08x\n", PED_BE32_TO_CPU(part->de_Interleave)); |
375 |
DPRINT(DEBUG_BLOCK, "\tde_LowCyl = %08x\n", PED_BE32_TO_CPU(part->de_LowCyl)); |
376 |
DPRINT(DEBUG_BLOCK, "\tde_HighCyl = %08x\n", PED_BE32_TO_CPU(part->de_HighCyl)); |
377 |
DPRINT(DEBUG_BLOCK, "\tde_NumBuffers = %08x\n", PED_BE32_TO_CPU(part->de_NumBuffers)); |
378 |
DPRINT(DEBUG_BLOCK, "\tde_BufMemType = %08x\n", PED_BE32_TO_CPU(part->de_BufMemType)); |
379 |
DPRINT(DEBUG_BLOCK, "\tde_MaxTransfer = %08x\n", PED_BE32_TO_CPU(part->de_MaxTransfer)); |
380 |
DPRINT(DEBUG_BLOCK, "\tde_Mask = %08x\n", PED_BE32_TO_CPU(part->de_Mask)); |
381 |
DPRINT(DEBUG_BLOCK, "\tde_BootPri = %08x\n", PED_BE32_TO_CPU(part->de_BootPri)); |
382 |
DPRINT(DEBUG_BLOCK, "\tde_DosType = %08x\n", PED_BE32_TO_CPU(part->de_DosType)); |
383 |
DPRINT(DEBUG_BLOCK, "\tde_Baud = %08x\n", PED_BE32_TO_CPU(part->de_Baud)); |
384 |
DPRINT(DEBUG_BLOCK, "\tde_Control = %08x\n", PED_BE32_TO_CPU(part->de_Control)); |
385 |
DPRINT(DEBUG_BLOCK, "\tde_BootBlocks = %08x\n", PED_BE32_TO_CPU(part->de_BootBlocks)); |
386 |
} |
387 |
|
388 |
#define AMIGA_MAX_PARTITIONS 128 |
389 |
|
390 |
struct LinkedBlock { |
391 |
uint32_t lk_ID; /* Identifier 32 bit word */ |
392 |
uint32_t lk_SummedLongs; /* Size of the structure for checksums */ |
393 |
int32_t lk_ChkSum; /* Checksum of the structure */ |
394 |
uint32_t pb_HostID; /* SCSI Target ID of host, not really used */ |
395 |
uint32_t lk_Next; /* Block number of the next PartitionBlock */ |
396 |
}; |
397 |
struct Linked2Block { |
398 |
uint32_t lk2_ID; /* Identifier 32 bit word */ |
399 |
uint32_t lk2_SummedLongs; /* Size of the structure for checksums */ |
400 |
int32_t lk2_ChkSum; /* Checksum of the structure */ |
401 |
uint32_t lk2_Next; /* Block number of the next PartitionBlock */ |
402 |
uint32_t lk2_Reverved[13]; |
403 |
uint32_t lk2_Linked; /* Secondary linked list */ |
404 |
}; |
405 |
#define LINK_END 0xffffffff |
406 |
#define LNK(pos) ((struct LinkedBlock *)(pos)) |
407 |
#define LNK2(pos) ((struct Linked2Block *)(pos)) |
408 |
|
409 |
void _amiga_dump_linked (struct LinkedBlock * block) { |
410 |
TRACE(); |
411 |
DPRINT(DEBUG_BLOCK, "LinkedBlock\n\tpart_ID = %08x %s\n", |
412 |
PED_BE32_TO_CPU(block->lk_ID), |
413 |
_amiga_block_id(PED_BE32_TO_CPU(block->lk_ID))); |
414 |
DPRINT(DEBUG_BLOCK, "\tlk_SummedLongs = %08x\n", |
415 |
PED_BE32_TO_CPU(block->lk_SummedLongs)); |
416 |
DPRINT(DEBUG_BLOCK, "\tlk_ChkSum = %08x (block checksum is %d)\n", |
417 |
block->lk_ChkSum, _amiga_checksum (AMIGA(block))); |
418 |
DPRINT(DEBUG_BLOCK, "\tlk_Next = %08x (%d)\n", |
419 |
PED_BE32_TO_CPU(block->lk_Next), PED_BE32_TO_CPU(block->lk_Next)); |
420 |
if (PED_BE32_TO_CPU(block->lk_ID)==IDNAME_FILESYSHEADER) { |
421 |
DPRINT(DEBUG_BLOCK, "\tlk2_Linked = %08x (%d)\n", |
422 |
PED_BE32_TO_CPU(LNK2(block)->lk2_Linked), |
423 |
PED_BE32_TO_CPU(LNK2(block)->lk2_Linked)); |
424 |
} |
425 |
} |
426 |
|
427 |
#define MAX_RDB_BLOCK (RDB_LOCATION_LIMIT + 2 * AMIGA_MAX_PARTITIONS + 2) |
428 |
|
429 |
static PedDiskType amiga_disk_type; |
430 |
|
431 |
static int |
432 |
amiga_probe (PedDevice *dev) |
433 |
{ |
434 |
struct RigidDiskBlock *rdb; |
435 |
uint32_t found; |
436 |
TRACE(); |
437 |
PED_ASSERT(dev!= NULL, return 0); |
438 |
|
439 |
if ((rdb=RDSK(ped_malloc(PED_SECTOR_SIZE)))==NULL) { |
440 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
441 |
PED_EXCEPTION_CANCEL, |
442 |
_("%s : Failed to allocate rdb block\n"), __func__)) |
443 |
{ |
444 |
case PED_EXCEPTION_CANCEL : |
445 |
case PED_EXCEPTION_UNHANDLED : |
446 |
default : |
447 |
return 0; |
448 |
} |
449 |
} |
450 |
found = _amiga_find_rdb (dev, rdb); |
451 |
ped_free (rdb); |
452 |
|
453 |
return (found == AMIGA_RDB_NOT_FOUND ? 0 : 1); |
454 |
} |
455 |
|
456 |
static PedDisk* |
457 |
amiga_alloc (PedDevice* dev) |
458 |
{ |
459 |
PedDisk *disk; |
460 |
struct AmigaDisk *adsk; |
461 |
struct RigidDiskBlock *rdb; |
462 |
int highest_cylinder, highest_block; |
463 |
TRACE(); |
464 |
PED_ASSERT(dev!= NULL, return NULL); |
465 |
|
466 |
if (!(disk = _ped_disk_alloc (dev, &amiga_disk_type))) { |
467 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
468 |
PED_EXCEPTION_CANCEL, |
469 |
_("%s : Failed to allocate disk structure\n"), __func__)) |
470 |
{ |
471 |
case PED_EXCEPTION_CANCEL : |
472 |
case PED_EXCEPTION_UNHANDLED : |
473 |
default : |
474 |
return NULL; |
475 |
} |
476 |
} |
477 |
|
478 |
if (!(disk->disk_specific = ped_malloc (PED_SECTOR_SIZE))) { |
479 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
480 |
PED_EXCEPTION_CANCEL, |
481 |
_("%s : Failed to allocate disk_specific rdb block\n"), __func__)) |
482 |
{ |
483 |
case PED_EXCEPTION_CANCEL : |
484 |
case PED_EXCEPTION_UNHANDLED : |
485 |
default : |
486 |
ped_free (disk); |
487 |
return NULL; |
488 |
} |
489 |
} |
490 |
rdb = disk->disk_specific; |
491 |
|
492 |
memset(rdb, -1, sizeof(struct RigidDiskBlock)); |
493 |
|
494 |
rdb->rdb_ID = PED_CPU_TO_BE32 (IDNAME_RIGIDDISK); |
495 |
rdb->rdb_SummedLongs = PED_CPU_TO_BE32 (64); |
496 |
rdb->rdb_HostID = PED_CPU_TO_BE32 (0); |
497 |
rdb->rdb_BlockBytes = PED_CPU_TO_BE32 (PED_SECTOR_SIZE); |
498 |
rdb->rdb_Flags = PED_CPU_TO_BE32 (0); |
499 |
|
500 |
/* Block lists */ |
501 |
rdb->rdb_BadBlockList = PED_CPU_TO_BE32 (0xffffffff); |
502 |
rdb->rdb_PartitionList = PED_CPU_TO_BE32 (0xffffffff); |
503 |
rdb->rdb_FileSysHeaderList = PED_CPU_TO_BE32 (0xffffffff); |
504 |
rdb->rdb_DriveInit = PED_CPU_TO_BE32 (0xffffffff); |
505 |
|
506 |
/* Physical drive characteristics */ |
507 |
rdb->rdb_Cylinders = PED_CPU_TO_BE32 (dev->cylinders); |
508 |
rdb->rdb_Sectors = PED_CPU_TO_BE32 (dev->sectors); |
509 |
rdb->rdb_Heads = PED_CPU_TO_BE32 (dev->heads); |
510 |
rdb->rdb_Interleave = PED_CPU_TO_BE32 (0); |
511 |
rdb->rdb_Park = PED_CPU_TO_BE32 (dev->cylinders); |
512 |
rdb->rdb_WritePreComp = PED_CPU_TO_BE32 (dev->cylinders); |
513 |
rdb->rdb_ReducedWrite = PED_CPU_TO_BE32 (dev->cylinders); |
514 |
rdb->rdb_StepRate = PED_CPU_TO_BE32 (0); |
515 |
|
516 |
highest_cylinder = MAX_RDB_BLOCK / (dev->sectors * dev->heads) + 1; |
517 |
highest_block = highest_cylinder * dev->sectors * dev->heads - 1; |
518 |
|
519 |
/* Logical driver characteristics */ |
520 |
rdb->rdb_RDBBlocksLo = PED_CPU_TO_BE32 (0); |
521 |
rdb->rdb_RDBBlocksHi = PED_CPU_TO_BE32 (highest_block); |
522 |
rdb->rdb_LoCylinder = PED_CPU_TO_BE32 (highest_cylinder); |
523 |
rdb->rdb_HiCylinder = PED_CPU_TO_BE32 (dev->cylinders -1); |
524 |
rdb->rdb_CylBlocks = PED_CPU_TO_BE32 (dev->sectors * dev->heads); |
525 |
rdb->rdb_AutoParkSeconds = PED_CPU_TO_BE32 (0); |
526 |
/* rdb_HighRDSKBlock will only be set when writing */ |
527 |
rdb->rdb_HighRDSKBlock = PED_CPU_TO_BE32 (0); |
528 |
|
529 |
/* Driver identification */ |
530 |
_amiga_set_bstr("", rdb->rdb_DiskVendor, 8); |
531 |
_amiga_set_bstr(dev->model, rdb->rdb_DiskProduct, 16); |
532 |
_amiga_set_bstr("", rdb->rdb_DiskRevision, 4); |
533 |
_amiga_set_bstr("", rdb->rdb_ControllerVendor, 8); |
534 |
_amiga_set_bstr("", rdb->rdb_ControllerProduct, 16); |
535 |
_amiga_set_bstr("", rdb->rdb_ControllerRevision, 4); |
536 |
|
537 |
/* And calculate the checksum */ |
538 |
_amiga_calculate_checksum ((struct AmigaBlock *) rdb); |
539 |
|
540 |
return disk; |
541 |
} |
542 |
|
543 |
static PedDisk* |
544 |
amiga_duplicate (const PedDisk* disk) |
545 |
{ |
546 |
PedDisk* new_disk; |
547 |
struct RigidDiskBlock * new_rdb; |
548 |
struct RigidDiskBlock * old_rdb; |
549 |
TRACE(); |
550 |
PED_ASSERT(disk!= NULL, return NULL); |
551 |
PED_ASSERT(disk->dev!= NULL, return NULL); |
552 |
PED_ASSERT(disk->disk_specific!= NULL, return NULL); |
553 |
|
554 |
old_rdb = (struct RigidDiskBlock *) disk->disk_specific; |
555 |
|
556 |
if (!(new_disk = ped_disk_new_fresh (disk->dev, &amiga_disk_type))) { |
557 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
558 |
PED_EXCEPTION_CANCEL, |
559 |
_("%s : Failed to allocate a new disk structure\n"), __func__)) |
560 |
{ |
561 |
case PED_EXCEPTION_CANCEL : |
562 |
case PED_EXCEPTION_UNHANDLED : |
563 |
default : |
564 |
return NULL; |
565 |
} |
566 |
} |
567 |
|
568 |
new_rdb = (struct RigidDiskBlock *) new_disk->disk_specific; |
569 |
memcpy (new_rdb, old_rdb, 256); |
570 |
return new_disk; |
571 |
} |
572 |
|
573 |
static void |
574 |
amiga_free (PedDisk* disk) |
575 |
{ |
576 |
TRACE(); |
577 |
PED_ASSERT(disk!= NULL, return); |
578 |
PED_ASSERT(disk->disk_specific!= NULL, return); |
579 |
|
580 |
ped_free (disk->disk_specific); |
581 |
_ped_disk_free (disk); |
582 |
} |
583 |
|
584 |
#ifndef DISCOVER_ONLY |
585 |
static int |
586 |
amiga_clobber (PedDevice* dev) |
587 |
{ |
588 |
struct RigidDiskBlock *rdb; |
589 |
uint32_t i; |
590 |
int result = 0; |
591 |
TRACE(); |
592 |
PED_ASSERT(dev!= NULL, return 0); |
593 |
|
594 |
if ((rdb=RDSK(ped_malloc(PED_SECTOR_SIZE)))==NULL) { |
595 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
596 |
PED_EXCEPTION_CANCEL, |
597 |
_("%s : Failed to allocate rdb block\n"), __func__)) |
598 |
{ |
599 |
case PED_EXCEPTION_CANCEL : |
600 |
case PED_EXCEPTION_UNHANDLED : |
601 |
default : |
602 |
return 0; |
603 |
} |
604 |
} |
605 |
|
606 |
while ((i = _amiga_find_rdb (dev, rdb)) != AMIGA_RDB_NOT_FOUND) { |
607 |
rdb->rdb_ID = PED_CPU_TO_BE32 (0); |
608 |
result = ped_device_write (dev, (void*) rdb, i, 1); |
609 |
} |
610 |
|
611 |
ped_free (rdb); |
612 |
|
613 |
return result; |
614 |
} |
615 |
#endif /* !DISCOVER_ONLY */ |
616 |
|
617 |
static int |
618 |
_amiga_loop_check (uint32_t block, uint32_t * blocklist, uint32_t max) |
619 |
{ |
620 |
uint32_t i; |
621 |
TRACE(); |
622 |
|
623 |
for (i = 0; i < max; i++) |
624 |
if (block == blocklist[i]) { |
625 |
/* We are looping, let's stop. */ |
626 |
return 1; |
627 |
} |
628 |
blocklist[max] = block; |
629 |
return 0; |
630 |
} |
631 |
|
632 |
/* We have already allocated a rdb, we are now reading it from the disk */ |
633 |
static int |
634 |
amiga_read (PedDisk* disk) |
635 |
{ |
636 |
struct RigidDiskBlock *rdb; |
637 |
struct PartitionBlock *partition; |
638 |
uint32_t partblock; |
639 |
uint32_t partlist[AMIGA_MAX_PARTITIONS]; |
640 |
PedSector cylblocks; |
641 |
int i; |
642 |
TRACE(); |
643 |
PED_ASSERT(disk!= NULL, return 0); |
644 |
PED_ASSERT(disk->dev!= NULL, return 0); |
645 |
PED_ASSERT(disk->disk_specific!= NULL, return 0); |
646 |
rdb = RDSK(disk->disk_specific); |
647 |
|
648 |
if (_amiga_find_rdb (disk->dev, rdb) == AMIGA_RDB_NOT_FOUND) { |
649 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
650 |
PED_EXCEPTION_CANCEL, |
651 |
_("%s : Didn't find rdb block, should never happen\n"), __func__)) |
652 |
{ |
653 |
case PED_EXCEPTION_CANCEL : |
654 |
case PED_EXCEPTION_UNHANDLED : |
655 |
default : |
656 |
return 0; |
657 |
} |
658 |
} |
659 |
#if (DEBUG_BLOCKS & MASTER_DEBUG) |
660 |
_amiga_dump_rdb(rdb); |
661 |
#endif |
662 |
|
663 |
/* Let's copy the rdb read geometry to the dev */ |
664 |
disk->dev->cylinders = PED_BE32_TO_CPU (rdb->rdb_Cylinders); |
665 |
disk->dev->heads = PED_BE32_TO_CPU (rdb->rdb_Heads); |
666 |
disk->dev->sectors = PED_BE32_TO_CPU (rdb->rdb_Sectors); |
667 |
cylblocks = (PedSector) PED_BE32_TO_CPU (rdb->rdb_Heads) * |
668 |
(PedSector) PED_BE32_TO_CPU (rdb->rdb_Sectors); |
669 |
|
670 |
/* Remove all partitions in the former in memory table */ |
671 |
ped_disk_delete_all (disk); |
672 |
|
673 |
/* Let's allocate a partition block */ |
674 |
if (!(partition = ped_malloc (PED_SECTOR_SIZE))) { |
675 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
676 |
PED_EXCEPTION_CANCEL, |
677 |
_("%s : Failed to allocate partition block\n"), __func__)) |
678 |
{ |
679 |
case PED_EXCEPTION_CANCEL : |
680 |
case PED_EXCEPTION_UNHANDLED : |
681 |
default : |
682 |
return 0; |
683 |
} |
684 |
} |
685 |
|
686 |
/* We initialize the hardblock free list to detect loops */ |
687 |
for (i = 0; i < AMIGA_MAX_PARTITIONS; i++) partlist[i] = 0xffffffff; |
688 |
|
689 |
for (i = 1, partblock = PED_BE32_TO_CPU(rdb->rdb_PartitionList); |
690 |
i < AMIGA_MAX_PARTITIONS && partblock != 0xffffffff; |
691 |
i++, partblock = PED_BE32_TO_CPU(partition->pb_Next)) |
692 |
{ |
693 |
PedPartition *part; |
694 |
PedSector start, end; |
695 |
struct DosEnvec *de; |
696 |
PedConstraint *constraint_exact; |
697 |
int j; |
698 |
|
699 |
/* Let's look for loops in the partition table */ |
700 |
if (_amiga_loop_check(partblock, partlist, i)) { |
701 |
break; |
702 |
} |
703 |
|
704 |
/* Let's allocate and read a partition block to get its geometry*/ |
705 |
if (!_amiga_read_block (disk->dev, AMIGA(partition), (PedSector)partblock)) { |
706 |
ped_free(partition); |
707 |
return 0; |
708 |
} |
709 |
#if (DEBUG_BLOCK & MASTER_DEBUG) |
710 |
_amiga_dump_part(partition); |
711 |
#endif |
712 |
|
713 |
start = ((PedSector) PED_BE32_TO_CPU (partition->de_LowCyl)) * cylblocks; |
714 |
end = ((((PedSector) PED_BE32_TO_CPU (partition->de_HighCyl))+1) * (cylblocks))-1; |
715 |
|
716 |
/* We can now construct a new partition */ |
717 |
if (!(part = ped_partition_new (disk, 0, NULL, start, end))) { |
718 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
719 |
PED_EXCEPTION_CANCEL, |
720 |
_("%s : Failed to allocate new partition (%llu -> %llu)\n"), |
721 |
__func__, start, end)) |
722 |
{ |
723 |
case PED_EXCEPTION_CANCEL : |
724 |
case PED_EXCEPTION_UNHANDLED : |
725 |
default : |
726 |
ped_free(partition); |
727 |
return 0; |
728 |
} |
729 |
} |
730 |
/* And copy over the partition block */ |
731 |
memcpy(part->disk_specific, partition, 256); |
732 |
|
733 |
part->num = i; |
734 |
part->type = 0; |
735 |
/* Let's probe what filesystem is present on the disk */ |
736 |
part->fs_type = ped_file_system_probe (&part->geom); |
737 |
|
738 |
constraint_exact = ped_constraint_exact (&part->geom); |
739 |
if (!ped_disk_add_partition (disk, part, constraint_exact)) { |
740 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
741 |
PED_EXCEPTION_CANCEL, |
742 |
_("%s : Failed to add new partition\n"), __func__)) |
743 |
{ |
744 |
case PED_EXCEPTION_CANCEL : |
745 |
case PED_EXCEPTION_UNHANDLED : |
746 |
default : |
747 |
ped_free(partition); |
748 |
return 0; |
749 |
} |
750 |
} |
751 |
ped_constraint_destroy (constraint_exact); |
752 |
} |
753 |
return 1; |
754 |
} |
755 |
|
756 |
static int |
757 |
_amiga_find_free_blocks(PedDisk *disk, uint32_t *table, |
758 |
struct LinkedBlock *block, uint32_t first, uint32_t type) |
759 |
{ |
760 |
PedSector next; |
761 |
TRACE(); |
762 |
PED_ASSERT(disk!= NULL, return 0); |
763 |
PED_ASSERT(disk->dev!= NULL, return 0); |
764 |
|
765 |
for (next = first; next != LINK_END; next = PED_BE32_TO_CPU(block->lk_Next)) { |
766 |
if (table[next] != IDNAME_FREE) { |
767 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
768 |
PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE | PED_EXCEPTION_CANCEL, |
769 |
_("%s : Loop detected at block %d\n"), __func__, next)) |
770 |
{ |
771 |
case PED_EXCEPTION_CANCEL : |
772 |
return 0; |
773 |
case PED_EXCEPTION_FIX : |
774 |
/* Need to add fixing code */ |
775 |
case PED_EXCEPTION_IGNORE : |
776 |
case PED_EXCEPTION_UNHANDLED : |
777 |
default : |
778 |
return 1; |
779 |
} |
780 |
} |
781 |
|
782 |
if (!_amiga_read_block (disk->dev, AMIGA(block), next)) { |
783 |
return 0; |
784 |
} |
785 |
#if (DEBUG_BLOCK & MASTER_DEBUG) |
786 |
_amiga_dump_linked(block); |
787 |
#endif |
788 |
if (PED_BE32_TO_CPU(block->lk_ID) != type) { |
789 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
790 |
PED_EXCEPTION_CANCEL, |
791 |
_("%s : The %s list seems bad at block %s\n"), |
792 |
__func__, _amiga_block_id(PED_BE32_TO_CPU(block->lk_ID)), next)) |
793 |
{ |
794 |
case PED_EXCEPTION_CANCEL : |
795 |
case PED_EXCEPTION_UNHANDLED : |
796 |
default : |
797 |
return 0; |
798 |
} |
799 |
} |
800 |
table[next] = type; |
801 |
if (PED_BE32_TO_CPU(block->lk_ID) == IDNAME_FILESYSHEADER) { |
802 |
if (_amiga_find_free_blocks(disk, table, block, |
803 |
PED_BE32_TO_CPU(LNK2(block)->lk2_Linked), |
804 |
IDNAME_LOADSEG) == 0) return 0; |
805 |
} |
806 |
} |
807 |
return 1; |
808 |
} |
809 |
static uint32_t |
810 |
_amiga_next_free_block(uint32_t *table, uint32_t start, uint32_t type) { |
811 |
int i; |
812 |
TRACE(); |
813 |
|
814 |
for (i = start; table[i] != type && table[i] != IDNAME_FREE; i++); |
815 |
return i; |
816 |
} |
817 |
static PedPartition * |
818 |
_amiga_next_real_partition(PedDisk *disk, PedPartition *part) { |
819 |
PedPartition *next; |
820 |
TRACE(); |
821 |
|
822 |
for (next = ped_disk_next_partition (disk, part); |
823 |
next != NULL && !ped_partition_is_active (next); |
824 |
next = ped_disk_next_partition (disk, next)); |
825 |
return next; |
826 |
} |
827 |
#ifndef DISCOVER_ONLY |
828 |
static int |
829 |
amiga_write (PedDisk* disk) |
830 |
{ |
831 |
struct RigidDiskBlock *rdb; |
832 |
struct LinkedBlock *block; |
833 |
struct PartitionBlock *partition; |
834 |
PedPartition *part, *next_part; |
835 |
PedSector cylblocks, first_hb, last_hb, last_used_hb; |
836 |
uint32_t * table; |
837 |
uint32_t i, rdb_block, max_part; |
838 |
uint32_t rdb_num, part_num, block_num, next_num; |
839 |
TRACE(); |
840 |
|
841 |
PED_ASSERT (disk != NULL, return 0;); |
842 |
PED_ASSERT (disk->dev != NULL, return 0;); |
843 |
PED_ASSERT (disk->disk_specific != NULL, return 0;); |
844 |
|
845 |
if (!(rdb = ped_malloc (PED_SECTOR_SIZE))) { |
846 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
847 |
PED_EXCEPTION_CANCEL, |
848 |
_("%s : Failed to allocate rdb block\n"), __func__)) |
849 |
{ |
850 |
case PED_EXCEPTION_CANCEL : |
851 |
case PED_EXCEPTION_UNHANDLED : |
852 |
default : |
853 |
return 0; |
854 |
} |
855 |
} |
856 |
/* Let's read the rdb */ |
857 |
if ((rdb_num = _amiga_find_rdb (disk->dev, rdb)) == AMIGA_RDB_NOT_FOUND) { |
858 |
rdb_num = 2; |
859 |
} else { |
860 |
memcpy (RDSK(disk->disk_specific), rdb, PED_SECTOR_SIZE); |
861 |
} |
862 |
ped_free (rdb); |
863 |
rdb = RDSK(disk->disk_specific); |
864 |
#if (DEBUG_BLOCK & MASTER_DEBUG) |
865 |
_amiga_dump_rdb(rdb); |
866 |
#endif |
867 |
|
868 |
/* Let's copy the rdb read geometry to the dev */ |
869 |
disk->dev->cylinders = PED_BE32_TO_CPU (rdb->rdb_Cylinders); |
870 |
disk->dev->heads = PED_BE32_TO_CPU (rdb->rdb_Heads); |
871 |
disk->dev->sectors = PED_BE32_TO_CPU (rdb->rdb_Sectors); |
872 |
cylblocks = (PedSector) PED_BE32_TO_CPU (rdb->rdb_Heads) * |
873 |
(PedSector) PED_BE32_TO_CPU (rdb->rdb_Sectors); |
874 |
first_hb = (PedSector) PED_BE32_TO_CPU (rdb->rdb_RDBBlocksLo); |
875 |
last_hb = (PedSector) PED_BE32_TO_CPU (rdb->rdb_RDBBlocksHi); |
876 |
last_used_hb = (PedSector) PED_BE32_TO_CPU (rdb->rdb_HighRDSKBlock); |
877 |
|
878 |
/* let's allocate a free block table and initialize it */ |
879 |
if (!(table = ped_malloc ((last_hb - first_hb + 1) * sizeof(uint32_t)))) { |
880 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
881 |
PED_EXCEPTION_CANCEL, |
882 |
_("%s : Failed to allocate free block table\n"), __func__)) |
883 |
{ |
884 |
case PED_EXCEPTION_CANCEL : |
885 |
case PED_EXCEPTION_UNHANDLED : |
886 |
default : |
887 |
return 0; |
888 |
} |
889 |
} |
890 |
memset(table, 0xff, (last_hb - first_hb + 1) * sizeof(uint32_t)); |
891 |
for (i = 0; i<=rdb_num; i++) table[i] = IDNAME_RIGIDDISK; |
892 |
|
893 |
/* Let's allocate a partition block */ |
894 |
if (!(block = ped_malloc (PED_SECTOR_SIZE))) { |
895 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
896 |
PED_EXCEPTION_CANCEL, |
897 |
_("%s : Failed to allocate partition block\n"), __func__)) |
898 |
{ |
899 |
case PED_EXCEPTION_CANCEL : |
900 |
case PED_EXCEPTION_UNHANDLED : |
901 |
default : |
902 |
ped_free (table); |
903 |
return 0; |
904 |
} |
905 |
} |
906 |
|
907 |
/* And fill the free block table */ |
908 |
if (_amiga_find_free_blocks(disk, table, block, |
909 |
PED_BE32_TO_CPU (rdb->rdb_BadBlockList), IDNAME_BADBLOCK) == 0) |
910 |
{ |
911 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
912 |
PED_EXCEPTION_CANCEL, |
913 |
_("%s : Failed to list badblocks\n"), __func__)) |
914 |
{ |
915 |
case PED_EXCEPTION_CANCEL : |
916 |
case PED_EXCEPTION_UNHANDLED : |
917 |
default : |
918 |
ped_free (table); |
919 |
ped_free (block); |
920 |
return 0; |
921 |
} |
922 |
} |
923 |
if (_amiga_find_free_blocks(disk, table, block, |
924 |
PED_BE32_TO_CPU (rdb->rdb_PartitionList), IDNAME_PARTITION) == 0) |
925 |
{ |
926 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
927 |
PED_EXCEPTION_CANCEL, |
928 |
_("%s : Failed to list partitionblocks\n"), __func__)) |
929 |
{ |
930 |
case PED_EXCEPTION_CANCEL : |
931 |
case PED_EXCEPTION_UNHANDLED : |
932 |
default : |
933 |
ped_free (table); |
934 |
ped_free (block); |
935 |
return 0; |
936 |
} |
937 |
} |
938 |
if (_amiga_find_free_blocks(disk, table, block, |
939 |
PED_BE32_TO_CPU (rdb->rdb_FileSysHeaderList), IDNAME_FILESYSHEADER) == 0) |
940 |
{ |
941 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
942 |
PED_EXCEPTION_CANCEL, |
943 |
_("%s : Failed to list filesystemblocks\n"), __func__)) |
944 |
{ |
945 |
case PED_EXCEPTION_CANCEL : |
946 |
case PED_EXCEPTION_UNHANDLED : |
947 |
default : |
948 |
ped_free (table); |
949 |
ped_free (block); |
950 |
return 0; |
951 |
} |
952 |
} |
953 |
if (_amiga_find_free_blocks(disk, table, block, |
954 |
PED_BE32_TO_CPU (rdb->rdb_BootBlockList), IDNAME_BOOT) == 0) |
955 |
{ |
956 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
957 |
PED_EXCEPTION_CANCEL, |
958 |
_("%s : Failed to list bootblocks\n"), __func__)) |
959 |
{ |
960 |
case PED_EXCEPTION_CANCEL : |
961 |
case PED_EXCEPTION_UNHANDLED : |
962 |
default : |
963 |
ped_free (table); |
964 |
ped_free (block); |
965 |
return 0; |
966 |
} |
967 |
} |
968 |
|
969 |
block_num = next_num = part_num = _amiga_next_free_block(table, rdb_num+1, IDNAME_PARTITION); |
970 |
part = _amiga_next_real_partition(disk, NULL); |
971 |
if (part == NULL) { |
972 |
rdb->rdb_PartitionList = PED_CPU_TO_BE32(LINK_END); |
973 |
} else { |
974 |
rdb->rdb_PartitionList = PED_CPU_TO_BE32(part_num); |
975 |
while (part != NULL) { |
976 |
PED_ASSERT(part->disk_specific!=NULL, return 0); |
977 |
next_part = _amiga_next_real_partition(disk, part); |
978 |
block_num = next_num; |
979 |
next_num = _amiga_next_free_block(table, block_num+1, IDNAME_PARTITION); |
980 |
partition = PART(part->disk_specific); |
981 |
partition->pb_Next = next_part==NULL ? PED_CPU_TO_BE32(LINK_END):PED_CPU_TO_BE32(next_num); |
982 |
partition->de_LowCyl = PED_CPU_TO_BE32(part->geom.start/cylblocks); |
983 |
partition->de_HighCyl = PED_CPU_TO_BE32((part->geom.end+1)/cylblocks-1); |
984 |
_amiga_calculate_checksum(AMIGA(partition)); |
985 |
if (!ped_device_write (disk->dev, (void*) partition, block_num, 1)) { |
986 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
987 |
PED_EXCEPTION_CANCEL, |
988 |
_("%s : Failed to write partition block at\n"), __func__, block_num)) |
989 |
{ |
990 |
case PED_EXCEPTION_CANCEL : |
991 |
case PED_EXCEPTION_UNHANDLED : |
992 |
default : |
993 |
ped_free (table); |
994 |
ped_free (block); |
995 |
/* WARNING : If we fail here, we stop everything, |
996 |
* and the partition table is lost. A Better |
997 |
* solution should be found, using the second |
998 |
* half of the hardblocks to not overwrite the |
999 |
* old partition table. It becomes problematic |
1000 |
* if we use more than half of the hardblocks. */ |
1001 |
return 0; |
1002 |
} |
1003 |
} |
1004 |
part = next_part; |
1005 |
} |
1006 |
} |
1007 |
if (block_num >PED_BE32_TO_CPU (rdb->rdb_HighRDSKBlock)) { |
1008 |
rdb->rdb_HighRDSKBlock = PED_CPU_TO_BE32(block_num); |
1009 |
} |
1010 |
_amiga_calculate_checksum(AMIGA(rdb)); |
1011 |
if (!ped_device_write (disk->dev, (void*) disk->disk_specific, rdb_num, 1)) { |
1012 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1013 |
PED_EXCEPTION_CANCEL, |
1014 |
_("%s : Failed to write rdb block at\n"), __func__, rdb_num)) |
1015 |
{ |
1016 |
case PED_EXCEPTION_CANCEL : |
1017 |
case PED_EXCEPTION_UNHANDLED : |
1018 |
default : |
1019 |
ped_free (table); |
1020 |
ped_free (block); |
1021 |
return 0; |
1022 |
} |
1023 |
} |
1024 |
|
1025 |
#if (DEBUG_BLOCK & MASTER_DEBUG) |
1026 |
_amiga_dump_rdb(rdb); |
1027 |
#endif |
1028 |
ped_free (table); |
1029 |
ped_free (block); |
1030 |
return ped_device_sync (disk->dev); |
1031 |
} |
1032 |
#endif /* !DISCOVER_ONLY */ |
1033 |
|
1034 |
static PedPartition* |
1035 |
amiga_partition_new (const PedDisk* disk, PedPartitionType part_type, |
1036 |
const PedFileSystemType* fs_type, |
1037 |
PedSector start, PedSector end) |
1038 |
{ |
1039 |
PedPartition *part; |
1040 |
PedDevice *dev; |
1041 |
PedSector cyl; |
1042 |
struct PartitionBlock *partition; |
1043 |
struct RigidDiskBlock *rdb; |
1044 |
TRACE(); |
1045 |
PED_ASSERT(disk!= NULL, return NULL); |
1046 |
PED_ASSERT(disk->dev!= NULL, return NULL); |
1047 |
PED_ASSERT(disk->disk_specific!= NULL, return NULL); |
1048 |
dev = disk->dev; |
1049 |
cyl = (PedSector) (dev->sectors * dev->heads); |
1050 |
rdb = RDSK(disk->disk_specific); |
1051 |
|
1052 |
if (!(part = _ped_partition_alloc (disk, part_type, fs_type, start, end))) { |
1053 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1054 |
PED_EXCEPTION_CANCEL, |
1055 |
_("%s : Failed to allocate a partition structure (%llu, %llu)"), |
1056 |
__func__, start, end)) |
1057 |
{ |
1058 |
case PED_EXCEPTION_CANCEL : |
1059 |
case PED_EXCEPTION_UNHANDLED : |
1060 |
default : |
1061 |
return NULL; |
1062 |
} |
1063 |
} |
1064 |
|
1065 |
|
1066 |
if (ped_partition_is_active (part)) { |
1067 |
if (!(part->disk_specific = ped_malloc (PED_SECTOR_SIZE))) { |
1068 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1069 |
PED_EXCEPTION_CANCEL, |
1070 |
_("%s : Failed to allocate a partition disk_specific block\n"), __func__)) |
1071 |
{ |
1072 |
case PED_EXCEPTION_CANCEL : |
1073 |
case PED_EXCEPTION_UNHANDLED : |
1074 |
default : |
1075 |
ped_free (part); |
1076 |
return NULL; |
1077 |
} |
1078 |
} |
1079 |
partition = PART(part->disk_specific); |
1080 |
memset(partition, -1, sizeof(struct PartitionBlock)); |
1081 |
|
1082 |
partition->pb_ID = PED_CPU_TO_BE32(IDNAME_PARTITION); |
1083 |
partition->pb_SummedLongs = PED_CPU_TO_BE32(64); |
1084 |
partition->pb_HostID = rdb->rdb_HostID; |
1085 |
partition->pb_Flags = PED_CPU_TO_BE32(0); |
1086 |
/* TODO : use a scheme including the device name and the |
1087 |
* partition number, if it is possible */ |
1088 |
_amiga_set_bstr("dhx", partition->pb_DriveName, 32); |
1089 |
|
1090 |
partition->de_TableSize = PED_CPU_TO_BE32(19); |
1091 |
partition->de_SizeBlock = PED_CPU_TO_BE32(128); |
1092 |
partition->de_SecOrg = PED_CPU_TO_BE32(0); |
1093 |
partition->de_Surfaces = PED_CPU_TO_BE32(dev->heads); |
1094 |
partition->de_SectorPerBlock = PED_CPU_TO_BE32(1); |
1095 |
partition->de_BlocksPerTrack = PED_CPU_TO_BE32(dev->sectors); |
1096 |
partition->de_Reserved = PED_CPU_TO_BE32(2); |
1097 |
partition->de_PreAlloc = PED_CPU_TO_BE32(0); |
1098 |
partition->de_Interleave = PED_CPU_TO_BE32(0); |
1099 |
partition->de_LowCyl = PED_CPU_TO_BE32(start/cyl); |
1100 |
partition->de_HighCyl = PED_CPU_TO_BE32((end+1)/cyl-1); |
1101 |
partition->de_NumBuffers = PED_CPU_TO_BE32(30); |
1102 |
partition->de_BufMemType = PED_CPU_TO_BE32(0); |
1103 |
partition->de_MaxTransfer = PED_CPU_TO_BE32(0x7fffffff); |
1104 |
partition->de_Mask = PED_CPU_TO_BE32(0xffffffff); |
1105 |
partition->de_BootPri = PED_CPU_TO_BE32(0); |
1106 |
partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); |
1107 |
partition->de_Baud = PED_CPU_TO_BE32(0); |
1108 |
partition->de_Control = PED_CPU_TO_BE32(0); |
1109 |
partition->de_BootBlocks = PED_CPU_TO_BE32(0); |
1110 |
|
1111 |
#if (DEBUG_BLOCK & MASTER_DEBUG) |
1112 |
_amiga_dump_part(partition); |
1113 |
#endif |
1114 |
} else { |
1115 |
part->disk_specific = NULL; |
1116 |
} |
1117 |
return part; |
1118 |
} |
1119 |
|
1120 |
static PedPartition* |
1121 |
amiga_partition_duplicate (const PedPartition* part) |
1122 |
{ |
1123 |
PedPartition *new_part; |
1124 |
struct PartitionBlock *new_amiga_part; |
1125 |
struct PartitionBlock *old_amiga_part; |
1126 |
TRACE(); |
1127 |
PED_ASSERT(part!= NULL, return NULL); |
1128 |
PED_ASSERT(part->disk!= NULL, return NULL); |
1129 |
PED_ASSERT(part->disk_specific!= NULL, return NULL); |
1130 |
old_amiga_part = (struct PartitionBlock *) part->disk_specific; |
1131 |
|
1132 |
new_part = ped_partition_new (part->disk, part->type, |
1133 |
part->fs_type, part->geom.start, |
1134 |
part->geom.end); |
1135 |
if (!new_part) { |
1136 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1137 |
PED_EXCEPTION_CANCEL, |
1138 |
_("%s : Failed to allocate a new partition structure\n"), __func__)) |
1139 |
{ |
1140 |
case PED_EXCEPTION_CANCEL : |
1141 |
case PED_EXCEPTION_UNHANDLED : |
1142 |
default : |
1143 |
return NULL; |
1144 |
} |
1145 |
} |
1146 |
|
1147 |
new_amiga_part = (struct PartitionBlock *) new_part->disk_specific; |
1148 |
memcpy (new_amiga_part, old_amiga_part, 256); |
1149 |
|
1150 |
return new_part; |
1151 |
} |
1152 |
|
1153 |
static void |
1154 |
amiga_partition_destroy (PedPartition* part) |
1155 |
{ |
1156 |
TRACE(); |
1157 |
PED_ASSERT (part != NULL, return); |
1158 |
|
1159 |
if (ped_partition_is_active (part)) { |
1160 |
PED_ASSERT (part->disk_specific!= NULL, return); |
1161 |
ped_free (part->disk_specific); |
1162 |
} |
1163 |
_ped_partition_free (part); |
1164 |
} |
1165 |
|
1166 |
static int |
1167 |
amiga_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type) |
1168 |
{ |
1169 |
struct PartitionBlock *partition; |
1170 |
TRACE(); |
1171 |
|
1172 |
PED_ASSERT (part != NULL, return 0); |
1173 |
PED_ASSERT (part->disk_specific != NULL, return 0); |
1174 |
partition = PART(part->disk_specific); |
1175 |
|
1176 |
part->fs_type = fs_type; |
1177 |
|
1178 |
if (!fs_type) |
1179 |
partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); /* 'LNX\0' */ |
1180 |
else if (!strcmp (fs_type->name, "ext2")) |
1181 |
partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); /* 'LNX\0' */ |
1182 |
else if (!strcmp (fs_type->name, "ext3")) |
1183 |
partition->de_DosType = PED_CPU_TO_BE32(0x45585403); /* 'EXT\3' */ |
1184 |
else if (!strcmp (fs_type->name, "linux-swap")) |
1185 |
partition->de_DosType = PED_CPU_TO_BE32(0x53575000); /* 'SWP\0' */ |
1186 |
else if (!strcmp (fs_type->name, "fat16")) |
1187 |
partition->de_DosType = PED_CPU_TO_BE32(0x46415400); /* 'FAT\0' */ |
1188 |
else if (!strcmp (fs_type->name, "fat32")) |
1189 |
partition->de_DosType = PED_CPU_TO_BE32(0x46415401); /* 'FAT\1'*/ |
1190 |
else if (!strcmp (fs_type->name, "hfs")) |
1191 |
partition->de_DosType = PED_CPU_TO_BE32(0x48465300); /* 'HFS\0' */ |
1192 |
else if (!strcmp (fs_type->name, "jfs")) |
1193 |
partition->de_DosType = PED_CPU_TO_BE32(0x4a465300); /* 'JFS\0' */ |
1194 |
else if (!strcmp (fs_type->name, "ntfs")) |
1195 |
partition->de_DosType = PED_CPU_TO_BE32(0x4e544653); /* 'NTFS' */ |
1196 |
else if (!strcmp (fs_type->name, "reiserfs")) |
1197 |
partition->de_DosType = PED_CPU_TO_BE32(0x52465300); /* 'RFS\0' */ |
1198 |
else if (!strcmp (fs_type->name, "sun-ufs")) |
1199 |
partition->de_DosType = PED_CPU_TO_BE32(0x53554653); /* 'SUFS' */ |
1200 |
else if (!strcmp (fs_type->name, "hp-ufs")) |
1201 |
partition->de_DosType = PED_CPU_TO_BE32(0x48554653); /* 'HUFS' */ |
1202 |
else if (!strcmp (fs_type->name, "xfs")) |
1203 |
partition->de_DosType = PED_CPU_TO_BE32(0x58465300); /* 'XFS\0' */ |
1204 |
else |
1205 |
partition->de_DosType = PED_CPU_TO_BE32(0x00000000); /* unknown */ |
1206 |
return 1; |
1207 |
} |
1208 |
|
1209 |
static int |
1210 |
amiga_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state) |
1211 |
{ |
1212 |
struct PartitionBlock *partition; |
1213 |
TRACE(); |
1214 |
|
1215 |
PED_ASSERT (part != NULL, return 0); |
1216 |
PED_ASSERT (part->disk_specific != NULL, return 0); |
1217 |
|
1218 |
partition = PART(part->disk_specific); |
1219 |
|
1220 |
switch (flag) { |
1221 |
case PED_PARTITION_BOOT: |
1222 |
if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_BOOTABLE); |
1223 |
else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_BOOTABLE)); |
1224 |
return 1; |
1225 |
case PED_PARTITION_HIDDEN: |
1226 |
if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_NOMOUNT); |
1227 |
else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_NOMOUNT)); |
1228 |
return 1; |
1229 |
default: |
1230 |
return 0; |
1231 |
} |
1232 |
} |
1233 |
|
1234 |
static int |
1235 |
amiga_partition_get_flag (const PedPartition* part, PedPartitionFlag flag) |
1236 |
{ |
1237 |
struct PartitionBlock *partition; |
1238 |
TRACE(); |
1239 |
|
1240 |
PED_ASSERT (part != NULL, return 0); |
1241 |
PED_ASSERT (part->disk_specific != NULL, return 0); |
1242 |
|
1243 |
partition = PART(part->disk_specific); |
1244 |
|
1245 |
switch (flag) { |
1246 |
case PED_PARTITION_BOOT: |
1247 |
return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_BOOTABLE)); |
1248 |
case PED_PARTITION_HIDDEN: |
1249 |
return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_NOMOUNT)); |
1250 |
default: |
1251 |
return 0; |
1252 |
} |
1253 |
} |
1254 |
|
1255 |
static int |
1256 |
amiga_partition_is_flag_available (const PedPartition* part, |
1257 |
PedPartitionFlag flag) |
1258 |
{ |
1259 |
TRACE(); |
1260 |
switch (flag) { |
1261 |
case PED_PARTITION_BOOT: |
1262 |
case PED_PARTITION_HIDDEN: |
1263 |
return 1; |
1264 |
default: |
1265 |
return 0; |
1266 |
} |
1267 |
} |
1268 |
|
1269 |
static void |
1270 |
amiga_partition_set_name (PedPartition* part, const char* name) |
1271 |
{ |
1272 |
struct PartitionBlock *partition; |
1273 |
TRACE(); |
1274 |
|
1275 |
PED_ASSERT (part != NULL, return); |
1276 |
PED_ASSERT (part->disk_specific != NULL, return); |
1277 |
|
1278 |
partition = PART(part->disk_specific); |
1279 |
_amiga_set_bstr(name, partition->pb_DriveName, 32); |
1280 |
} |
1281 |
static const char* |
1282 |
amiga_partition_get_name (const PedPartition* part) |
1283 |
{ |
1284 |
struct PartitionBlock *partition; |
1285 |
TRACE(); |
1286 |
|
1287 |
PED_ASSERT (part != NULL, return 0); |
1288 |
PED_ASSERT (part->disk_specific != NULL, return 0); |
1289 |
|
1290 |
partition = PART(part->disk_specific); |
1291 |
|
1292 |
return _amiga_get_bstr(partition->pb_DriveName); |
1293 |
} |
1294 |
|
1295 |
static PedConstraint* |
1296 |
_amiga_get_constraint (const PedDisk *disk) |
1297 |
{ |
1298 |
PedDevice *dev = disk->dev; |
1299 |
PedAlignment start_align, end_align; |
1300 |
PedGeometry max_geom; |
1301 |
struct RigidDiskBlock *rdb = RDSK(disk->disk_specific); |
1302 |
PedSector cyl = (PedSector) PED_BE32_TO_CPU(rdb->rdb_Sectors) * |
1303 |
PED_BE32_TO_CPU(rdb->rdb_Heads); |
1304 |
PedSector start = ((273 / cyl) + 1) * cyl; |
1305 |
PedSector length = dev->length - start; |
1306 |
PedSector max_part = (length / cyl) * cyl; |
1307 |
TRACE(); |
1308 |
|
1309 |
if (!ped_alignment_init(&start_align, 0, cyl)) { |
1310 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1311 |
PED_EXCEPTION_CANCEL, |
1312 |
_("%s : Failed to initialize the start constraint\n"), __func__)) |
1313 |
{ |
1314 |
case PED_EXCEPTION_CANCEL : |
1315 |
case PED_EXCEPTION_UNHANDLED : |
1316 |
default : |
1317 |
return NULL; |
1318 |
} |
1319 |
} |
1320 |
if (!ped_alignment_init(&end_align, -1, cyl)) { |
1321 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1322 |
PED_EXCEPTION_CANCEL, |
1323 |
_("%s : Failed to initialize the end constraint\n"), __func__)) |
1324 |
{ |
1325 |
case PED_EXCEPTION_CANCEL : |
1326 |
case PED_EXCEPTION_UNHANDLED : |
1327 |
default : |
1328 |
return NULL; |
1329 |
} |
1330 |
} |
1331 |
if (!ped_geometry_init(&max_geom, dev, start, length)) { |
1332 |
switch (ped_exception_throw(PED_EXCEPTION_ERROR, |
1333 |
PED_EXCEPTION_CANCEL, |
1334 |
_("%s : Failed to initialize the max constraint\n"), __func__)) |
1335 |
{ |
1336 |
case PED_EXCEPTION_CANCEL : |
1337 |
case PED_EXCEPTION_UNHANDLED : |
1338 |
default : |
1339 |
return NULL; |
1340 |
} |
1341 |
} |
1342 |
|
1343 |
return ped_constraint_new (&start_align, &end_align, |
1344 |
&max_geom, &max_geom, cyl, max_part); |
1345 |
} |
1346 |
|
1347 |
static void |
1348 |
_amiga_dump_constraint (const PedConstraint* constraint) |
1349 |
{ |
1350 |
DPRINT (DEBUG_CONST, "Align Start : offset = %lld, grain = %lld\n", |
1351 |
constraint->start_align->offset, |
1352 |
constraint->start_align->grain_size); |
1353 |
DPRINT (DEBUG_CONST, "Align End : offset = %lld, grain = %lld\n", |
1354 |
constraint->end_align->offset, |
1355 |
constraint->end_align->grain_size); |
1356 |
DPRINT (DEBUG_CONST, "Range Start : start = %lld, length = %lld, end = %lld\n", |
1357 |
constraint->start_range->start, |
1358 |
constraint->start_range->length, |
1359 |
constraint->start_range->end); |
1360 |
DPRINT (DEBUG_CONST, "Range End : start = %lld, length = %lld, end = %lld\n", |
1361 |
constraint->end_range->start, |
1362 |
constraint->end_range->length, |
1363 |
constraint->end_range->end); |
1364 |
DPRINT (DEBUG_CONST, "Size : min = %lld, Max = %lld\n", |
1365 |
constraint->min_size, constraint->max_size); |
1366 |
} |
1367 |
|
1368 |
static int |
1369 |
amiga_partition_align (PedPartition* part, const PedConstraint* constraint) |
1370 |
{ |
1371 |
TRACE(); |
1372 |
|
1373 |
PED_ASSERT (part != NULL, return 0); |
1374 |
PED_ASSERT (part->disk != NULL, return 0); |
1375 |
|
1376 |
#if (DEBUG_CONST & MASTER_DEBUG) |
1377 |
_amiga_dump_constraint (constraint); |
1378 |
_amiga_dump_constraint (_amiga_get_constraint (part->disk)); |
1379 |
#endif |
1380 |
if (_ped_partition_attempt_align (part, constraint, |
1381 |
_amiga_get_constraint (part->disk))) |
1382 |
{ |
1383 |
return 1; |
1384 |
} |
1385 |
|
1386 |
#ifndef DISCOVER_ONLY |
1387 |
ped_exception_throw ( |
1388 |
PED_EXCEPTION_ERROR, |
1389 |
PED_EXCEPTION_CANCEL, |
1390 |
_("%s : Unable to satisfy all constraints on the partition."), __func__); |
1391 |
#endif |
1392 |
return 0; |
1393 |
} |
1394 |
|
1395 |
static int |
1396 |
amiga_partition_enumerate (PedPartition* part) |
1397 |
{ |
1398 |
|
1399 |
int i; |
1400 |
PedPartition* p; |
1401 |
TRACE(); |
1402 |
|
1403 |
PED_ASSERT (part != NULL, return 0); |
1404 |
PED_ASSERT (part->disk != NULL, return 0); |
1405 |
|
1406 |
/* never change the partition numbers */ |
1407 |
if (part->num != -1) |
1408 |
return 1; |
1409 |
for (i = 1; i <= AMIGA_MAX_PARTITIONS; i++) { |
1410 |
p = ped_disk_get_partition (part->disk, i); |
1411 |
if (!p) { |
1412 |
part->num = i; |
1413 |
return 1; |
1414 |
} |
1415 |
} |
1416 |
|
1417 |
/* failed to allocate a number */ |
1418 |
#ifndef DISCOVER_ONLY |
1419 |
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, |
1420 |
_("%s : Unable to allocate a partition"), __func__); |
1421 |
#endif |
1422 |
return 0; |
1423 |
} |
1424 |
|
1425 |
static int |
1426 |
amiga_alloc_metadata (PedDisk* disk) |
1427 |
{ |
1428 |
PedPartition* new_part; |
1429 |
PedConstraint* constraint_any = NULL; |
1430 |
PedSector highest_block; |
1431 |
TRACE(); |
1432 |
|
1433 |
PED_ASSERT (disk != NULL, goto error); |
1434 |
PED_ASSERT (disk->dev != NULL, goto error); |
1435 |
|
1436 |
constraint_any = ped_constraint_any (disk->dev); |
1437 |
|
1438 |
/* Allocate space for the RDB */ |
1439 |
new_part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL, 0, MAX_RDB_BLOCK); |
1440 |
if (!new_part) |
1441 |
goto error; |
1442 |
|
1443 |
if (!ped_disk_add_partition (disk, new_part, constraint_any)) { |
1444 |
ped_partition_destroy (new_part); |
1445 |
goto error; |
1446 |
} |
1447 |
|
1448 |
ped_constraint_destroy (constraint_any); |
1449 |
return 1; |
1450 |
error: |
1451 |
ped_constraint_destroy (constraint_any); |
1452 |
return 0; |
1453 |
} |
1454 |
|
1455 |
static int |
1456 |
amiga_get_max_primary_partition_count (const PedDisk* disk) |
1457 |
{ |
1458 |
TRACE(); |
1459 |
return AMIGA_MAX_PARTITIONS; |
1460 |
} |
1461 |
|
1462 |
static PedDiskOps amiga_disk_ops = { |
1463 |
probe: amiga_probe, |
1464 |
#ifndef DISCOVER_ONLY |
1465 |
clobber: amiga_clobber, |
1466 |
#else |
1467 |
clobber: NULL, |
1468 |
#endif |
1469 |
alloc: amiga_alloc, |
1470 |
duplicate: amiga_duplicate, |
1471 |
free: amiga_free, |
1472 |
read: amiga_read, |
1473 |
#ifndef DISCOVER_ONLY |
1474 |
write: amiga_write, |
1475 |
#else |
1476 |
write: NULL, |
1477 |
#endif |
1478 |
|
1479 |
partition_new: amiga_partition_new, |
1480 |
partition_duplicate: amiga_partition_duplicate, |
1481 |
partition_destroy: amiga_partition_destroy, |
1482 |
partition_set_system: amiga_partition_set_system, |
1483 |
partition_set_flag: amiga_partition_set_flag, |
1484 |
partition_get_flag: amiga_partition_get_flag, |
1485 |
partition_is_flag_available: |
1486 |
amiga_partition_is_flag_available, |
1487 |
partition_set_name: amiga_partition_set_name, |
1488 |
partition_get_name: amiga_partition_get_name, |
1489 |
partition_align: amiga_partition_align, |
1490 |
partition_enumerate: amiga_partition_enumerate, |
1491 |
|
1492 |
|
1493 |
alloc_metadata: amiga_alloc_metadata, |
1494 |
get_max_primary_partition_count: |
1495 |
amiga_get_max_primary_partition_count |
1496 |
}; |
1497 |
|
1498 |
static PedDiskType amiga_disk_type = { |
1499 |
next: NULL, |
1500 |
name: "amiga", |
1501 |
ops: &amiga_disk_ops, |
1502 |
features: PED_DISK_TYPE_PARTITION_NAME |
1503 |
}; |
1504 |
|
1505 |
void |
1506 |
ped_disk_amiga_init () |
1507 |
{ |
1508 |
TRACE(); |
1509 |
PED_ASSERT(sizeof(struct AmigaBlock) != 3, return); |
1510 |
PED_ASSERT(sizeof(struct RigidDiskBlock) != 64, return); |
1511 |
PED_ASSERT(sizeof(struct PartitionBlock) != 64, return); |
1512 |
PED_ASSERT(sizeof(struct LinkedBlock) != 5, return); |
1513 |
PED_ASSERT(sizeof(struct Linked2Block) != 18, return); |
1514 |
|
1515 |
ped_register_disk_type (&amiga_disk_type); |
1516 |
} |
1517 |
|
1518 |
void |
1519 |
ped_disk_amiga_done () |
1520 |
{ |
1521 |
TRACE(); |
1522 |
|
1523 |
ped_unregister_disk_type (&amiga_disk_type); |
1524 |
} |