16#include "hdsk-devctl.h"
20extern u32 TotalCopied;
39 for (partMax = deviceinfo[device].partitionMaxSize; 0x0003FFFF < partMax; partMax = deviceinfo[device].partitionMaxSize) {
40 for (; 0x0003FFFF < partMax; partMax /= 2) {
42 if ((sectors % partMax == 0) && ((u64)sectors + partMax < deviceinfo[device].totalLBA)) {
43 if ((0x001FFFFF < partMax) || (stat->free & partMax) == 0) {
44 stat->free += partMax;
51 if (0x0003FFFF >= partMax) {
56 APA_PRINTF(APA_DRV_NAME
": total = %08lx sectors, installable = %08lx sectors.\n", sectors, stat->free);
59static void hdskSimClearHeader(
struct hdskBitmap *header)
71 if (prev->type == 0) {
72 length = prev->length + part->length;
73 if (prev->start % length != 0) {
77 if ((length & (length - 1)) == 0) {
78 prev->length = length;
100 if (next->type == 0) {
101 length = next->length + part->length;
102 if (part->start % length != 0) {
106 if ((length & (length - 1)) == 0) {
107 part->length = length;
121static void hdskSimSwapPartition(
struct hdskBitmap *start)
123 u32 OrigStart, OrigLen;
134 }
while (part->type != 0);
138 OrigStart = start->start;
139 OrigLen = start->length;
140 for (i = 0; i < 2; i++) {
141 start = hdskSimDeleteFixNext(hdskSimDeleteFixPrev(start));
144 if (start->start == OrigStart && start->length == OrigLen) {
152 APA_PRINTF(
"MovePartition: %08lx to %08lx. sector count = %08lx.\n", start->start, dest->start, start->length);
154 TotalCopied += start->length;
155 APA_PRINTF(
"swap partition start...");
157 dest->type = start->type;
158 hdskSimSwapPartition(start);
161 APA_PRINTF(
"MovePartition: done\n");
164struct hdskBitmap *hdskSimFindEmptyPartition(u32 size)
169 if (part->length == size && part->type == 0) {
170 APA_PRINTF(
"sim: found empty partition at %08lx, size %08lx.\n", part->start, part->length);
178static int hdskCheckIfPrevPartEmpty(
struct hdskBitmap *part, u32 size)
180 return ((part->prev->length == size) && (part->prev->type == 0));
183static int hdskCheckIfNextPartEmpty(
struct hdskBitmap *part, u32 size)
185 return ((part->next->length == size) && (part->next->type == 0));
188struct hdskBitmap *hdskSimFindLastUsedPartition(u32 size, u32 start,
int mode)
193 SliceSize = size * 2;
195 if (part->length == size && part->type != 0) {
197 if (part->start % SliceSize != 0) {
198 if (hdskCheckIfPrevPartEmpty(part, size) != 0) {
202 if (part ==
hdskBitmap[0].prev || hdskCheckIfNextPartEmpty(part, size) != 0) {
215static int hdskCheckIsLastPart(
struct hdskBitmap *part, u32 size,
int mode)
220 for (CurrPart = part, PartSize = part->length; (PartSize < size) && (CurrPart !=
hdskBitmap); PartSize += CurrPart->length) {
221 CurrPart = CurrPart->next;
222 if (CurrPart->type == 0) {
227 if (PartSize == size) {
229 if (part->start % (size * 2) != 0) {
230 return (hdskCheckIfPrevPartEmpty(part, size) == 0 ? 0 : 1);
232 return (CurrPart !=
hdskBitmap[0].prev ? (hdskCheckIfNextPartEmpty(CurrPart, size) != 0) : 1);
242struct hdskBitmap *hdskSimFindLastUsedBlock(u32 size, u32 start,
int mode)
247 if ((part->start % size == 0) && (part->length < size) && (part->type != 0)) {
248 if (hdskCheckIsLastPart(part, size, mode) != 0) {
257static struct hdskBitmap *hdskBitmapAlloc(
void)
262 for (part = &
hdskBitmap[1], i = 1; i < HDSK_BITMAP_SIZE; i++, part++) {
263 if (part->length == 0) {
271static void hdskSimSplitEmptyPartition(
struct hdskBitmap *part, u32 length)
273 APA_PRINTF(
"split empty partition.\n");
274 while (part->length != length) {
277 end = hdskBitmapAlloc();
279 end->start = part->start + part->length;
281 end->length = part->length;
291 APA_PRINTF(
"MovePartitionsBlock: %08lx to %08lx. sector count = %08lx.\n", src->start, dest->start, dest->length);
293 hdskSimSplitEmptyPartition(dest, src->length);
298 hdskSimMovePartition(dest, src);
300 size -= dest->length;
308 APA_PRINTF(
"MovePartitionsBlock: done\n");