16#include "hdsk-devctl.h"
20extern u32 TotalCopied;
30 u32 maxsize = 0x1FFFFF;
31 u32 minsize = 0x3FFFF;
32#ifdef APA_8MB_PARTITION_SIZE
44 for (partMax = deviceinfo[device].partitionMaxSize; minsize < partMax; partMax = deviceinfo[device].partitionMaxSize) {
45 for (; minsize < partMax; partMax /= 2) {
47 if ((sectors % partMax == 0) && ((u64)sectors + partMax < deviceinfo[device].totalLBA)) {
48 if ((maxsize < partMax) || (stat->free & partMax) == 0) {
49 stat->free += partMax;
56 if (minsize >= partMax) {
61 APA_PRINTF(APA_DRV_NAME
": total = %08lx sectors, installable = %08lx sectors.\n", sectors, stat->free);
64static void hdskSimClearHeader(
struct hdskBitmap *header)
76 if (prev->type == 0) {
77 length = prev->length + part->length;
78 if (prev->start % length != 0) {
82 if ((length & (length - 1)) == 0) {
83 prev->length = length;
105 if (next->type == 0) {
106 length = next->length + part->length;
107 if (part->start % length != 0) {
111 if ((length & (length - 1)) == 0) {
112 part->length = length;
126static void hdskSimSwapPartition(
struct hdskBitmap *start)
128 u32 OrigStart, OrigLen;
139 }
while (part->type != 0);
143 OrigStart = start->start;
144 OrigLen = start->length;
145 for (i = 0; i < 2; i++) {
146 start = hdskSimDeleteFixNext(hdskSimDeleteFixPrev(start));
149 if (start->start == OrigStart && start->length == OrigLen) {
157 APA_PRINTF(
"MovePartition: %08lx to %08lx. sector count = %08lx.\n", start->start, dest->start, start->length);
159 TotalCopied += start->length;
160 APA_PRINTF(
"swap partition start...");
162 dest->type = start->type;
163 hdskSimSwapPartition(start);
166 APA_PRINTF(
"MovePartition: done\n");
169struct hdskBitmap *hdskSimFindEmptyPartition(u32 size)
174 if (part->length == size && part->type == 0) {
175 APA_PRINTF(
"sim: found empty partition at %08lx, size %08lx.\n", part->start, part->length);
183static int hdskCheckIfPrevPartEmpty(
struct hdskBitmap *part, u32 size)
185 return ((part->prev->length == size) && (part->prev->type == 0));
188static int hdskCheckIfNextPartEmpty(
struct hdskBitmap *part, u32 size)
190 return ((part->next->length == size) && (part->next->type == 0));
193struct hdskBitmap *hdskSimFindLastUsedPartition(u32 size, u32 start,
int mode)
198 SliceSize = size * 2;
200 if (part->length == size && part->type != 0) {
202 if (part->start % SliceSize != 0) {
203 if (hdskCheckIfPrevPartEmpty(part, size) != 0) {
207 if (part ==
hdskBitmap[0].prev || hdskCheckIfNextPartEmpty(part, size) != 0) {
220static int hdskCheckIsLastPart(
struct hdskBitmap *part, u32 size,
int mode)
225 for (CurrPart = part, PartSize = part->length; (PartSize < size) && (CurrPart !=
hdskBitmap); PartSize += CurrPart->length) {
226 CurrPart = CurrPart->next;
227 if (CurrPart->type == 0) {
232 if (PartSize == size) {
234 if (part->start % (size * 2) != 0) {
235 return (hdskCheckIfPrevPartEmpty(part, size) == 0 ? 0 : 1);
237 return (CurrPart !=
hdskBitmap[0].prev ? (hdskCheckIfNextPartEmpty(CurrPart, size) != 0) : 1);
247struct hdskBitmap *hdskSimFindLastUsedBlock(u32 size, u32 start,
int mode)
252 if ((part->start % size == 0) && (part->length < size) && (part->type != 0)) {
253 if (hdskCheckIsLastPart(part, size, mode) != 0) {
262static struct hdskBitmap *hdskBitmapAlloc(
void)
267 for (part = &
hdskBitmap[1], i = 1; i < HDSK_BITMAP_SIZE; i++, part++) {
268 if (part->length == 0) {
276static void hdskSimSplitEmptyPartition(
struct hdskBitmap *part, u32 length)
278 APA_PRINTF(
"split empty partition.\n");
279 while (part->length != length) {
282 end = hdskBitmapAlloc();
284 end->start = part->start + part->length;
286 end->length = part->length;
296 APA_PRINTF(
"MovePartitionsBlock: %08lx to %08lx. sector count = %08lx.\n", src->start, dest->start, dest->length);
298 hdskSimSplitEmptyPartition(dest, src->length);
303 hdskSimMovePartition(dest, src);
305 size -= dest->length;
313 APA_PRINTF(
"MovePartitionsBlock: done\n");