28#define NEWLIB_PORT_AWARE
29#include "fileXio_rpc.h"
32#define PFS_ZONE_SIZE 8192
33#define PFS_FRAGMENT_OPT 0x00002d66
34#define PFS_FRAGMENT 0x00000000
36#if (PFS_FRAGMENT != 0)
37static const int pfsFormatArg[3] = { PFS_ZONE_SIZE, PFS_FRAGMENT_OPT, PFS_FRAGMENT };
39static const int pfsFormatArg[1] = { PFS_ZONE_SIZE };
42#define _OMIT_SYSTEM_PARTITION
45static char *sizesString[9] = {
57static int sizesMB[9] = {
69static void hddUpdateInfo();
71static int hddStatusCurrent = 0;
72static u32 hddSize, hddFree, hddMaxPartitionSize;
81 rv = fileXioDevctl(
"hdd0:", HDIOC_STATUS, NULL, 0, NULL, 0);
83 if((rv >= 3) || (rv < 0))
89int hddCheckFormatted()
96 rv = fileXioDevctl(
"hdd0:", HDIOC_STATUS, NULL, 0, NULL, 0);
97 if((rv >= 1) || (rv < 0))
107 const char *partitionList[] = {
115 if(!hddStatusCurrent)
118 retVal = fileXioFormat(
"hdd0:", NULL, NULL, 0);
122 for(i = 0; partitionList[i] != NULL; i++)
124 retVal = fileXioFormat(
"pfs:", partitionList[i], (
const char*)&pfsFormatArg,
sizeof(pfsFormatArg));
142 if(!hddStatusCurrent)
145 hddFd = fileXioDopen(
"hdd0:");
150 rv = fileXioDread(hddFd, &dirEnt);
152 while((rv > 0) && (count < maxEntries))
156 u32 zoneFree, zoneSize;
159 if((dirEnt.stat.attr & ATTR_SUB_PARTITION) || (dirEnt.stat.mode == FS_TYPE_EMPTY))
161 rv = fileXioDread(hddFd, &dirEnt);
166 snprintf(hddFs[count].filename,
sizeof(hddFs[count].filename),
"hdd0:%.34s", dirEnt.name);
169 if((dirEnt.name[0] ==
'_') && (dirEnt.name[1] ==
'_'))
172 strcpy(hddFs[count].name, &dirEnt.name[2]);
174 else if(dirEnt.name[0] == FS_COMMON_PREFIX)
177 strcpy(hddFs[count].name, &dirEnt.name[1]);
182 strcpy(hddFs[count].name, dirEnt.name);
185#ifdef _OMIT_SYSTEM_PARTITION
186 if(hddFs[count].fileSystemGroup == FS_GROUP_SYSTEM)
188 rv = fileXioDread(hddFd, &dirEnt);
194 printf(
"> Filename: %s\n> Name: %s\n> Type: %d\n", hddFs[count].filename, hddFs[count].name, hddFs[count].fileSystemGroup);
198 partitionFd = fileXioOpen(hddFs[count].filename, FIO_O_RDONLY);
205 rv = fileXioDread(hddFd, &dirEnt);
209 for(i = 0, size = 0; (
unsigned int)i < dirEnt.stat.private_0 + 1; i++)
211 rv = fileXioIoctl2(partitionFd, HIOCGETSIZE, &i, 4, NULL, 0);
212 size += (u32)rv / 2048;
215 fileXioClose(partitionFd);
217 hddFs[count].
size = size;
223 if(dirEnt.stat.mode == FS_TYPE_PFS)
225 rv = fileXioMount(
"pfs0:", hddFs[count].filename, FIO_MT_RDONLY);
229 zoneFree = fileXioDevctl(
"pfs0:", PDIOC_ZONEFREE, NULL, 0, NULL, 0);
230 zoneSize = fileXioDevctl(
"pfs0:", PDIOC_ZONESZ, NULL, 0, NULL, 0);
232 hddFs[count].
freeSpace = zoneFree * zoneSize / 1024 / 1024;
235 fileXioUmount(
"pfs0:");
240 printf(
"> Formatted: %d\n> Size: %d\n> Free: %d\n", hddFs[count].formatted, (
int)hddFs[count].size, (
int)hddFs[count].freeSpace);
244 rv = fileXioDread(hddFd, &dirEnt);
247 fileXioDclose(hddFd);
261static void hddUpdateInfo()
263 iox_dirent_t infoDirEnt;
268 hddSize = (u32)fileXioDevctl(
"hdd0:", HDIOC_TOTALSECTOR, NULL, 0, NULL, 0) / 2048;
274 hddFd = fileXioDopen(
"hdd0:");
278 rv = fileXioDread(hddFd, &infoDirEnt);
281 if(infoDirEnt.stat.mode != FS_TYPE_EMPTY)
282 hddUsed += infoDirEnt.stat.size / 2048;
284 rv = fileXioDread(hddFd, &infoDirEnt);
286 fileXioDclose(hddFd);
287 hddFree = hddSize - hddUsed;
289 hddMaxPartitionSize = (u32)fileXioDevctl(
"hdd0:", HDIOC_MAXSECTOR, NULL, 0, NULL, 0) / 2048;
291 hddStatusCurrent = 1;
294int hddMakeFilesystem(
int fsSizeMB,
char *name,
int type)
299 int fsSizeLeft = fsSizeMB;
301 char openString[268];
305 if(!hddStatusCurrent)
313 case FS_GROUP_SYSTEM:
314 snprintf(fsName,
sizeof(fsName),
"__%s", name);
316 case FS_GROUP_COMMON:
317 snprintf(fsName,
sizeof(fsName),
"+%s", name);
320 strlcpy(fsName, name,
sizeof(fsName));
325 snprintf(openString,
sizeof(openString),
"hdd0:%.*s", (
int)(
sizeof(fsName) - 1), fsName);
326 partFd = fileXioOpen(openString, FIO_O_RDONLY);
327 if(partFd > 0 || partFd == -EACCES)
329 fileXioClose(partFd);
334 for(maxIndex = 0; maxIndex < 9; maxIndex++)
335 if((u32)(sizesMB[maxIndex]) == hddMaxPartitionSize)
339 for(useIndex = maxIndex; sizesMB[useIndex] > fsSizeMB; useIndex--);
341 for(partSize = sizesMB[useIndex]; useIndex >= 0; useIndex--,partSize = sizesMB[useIndex])
344 printf(
">>> Attempting to create main partition, size %d MB\n", partSize);
347 snprintf(openString,
sizeof(openString),
"hdd0:%.*s,,,%s,PFS", (
int)(
sizeof(fsName) - 1), fsName, sizesString[useIndex]);
349 printf(
">>> openString = %s\n", openString);
352 partFd = fileXioOpen(openString, FIO_O_RDWR | FIO_O_CREAT);
356 if(partFd != -ENOSPC)
359 printf(
">>> Could not create Main Partition (error %d)!\n", partFd);
369 printf(
">>> Could not create Main Partition (no space)!\n");
374 fsSizeLeft -= partSize;
376 printf(
">>> Main partition of %d MB created!\n", partSize);
383 if(fsSizeLeft < partSize)
386 printf(
">>> Adjusting sub size: %d MB to ", sizesMB[useIndex]);
388 for(useIndex = maxIndex; sizesMB[useIndex] > fsSizeLeft; useIndex--);
389 partSize = sizesMB[useIndex];
392 printf(
"%d MB\n", sizesMB[useIndex]);
398 printf(
">>> Attempting to create sub partition of size %d MB\n", sizesMB[useIndex]);
400 retVal = fileXioIoctl2(partFd, HIOCADDSUB, sizesString[useIndex], strlen(sizesString[useIndex]) + 1, NULL, 0);
401 if(retVal == -ENOSPC)
406 partSize = sizesMB[useIndex];
412 printf(
">>> Out of sizes to try. Giving up.\n");
417 printf(
">>> Subpartition alloc FAILED! Trying with size of %d MB\n", partSize);
423 else if(retVal == -EFBIG)
428 printf(
">>> Sub creation successfull!\n");
434 printf(
">>> Unknown error while creating sub: %d\n", retVal);
438 fsSizeLeft -= sizesMB[useIndex];
441 fileXioClose(partFd);
443 snprintf(openString,
sizeof(openString),
"hdd0:%.*s", (
int)(
sizeof(fsName) - 1), fsName);
444 retVal = fileXioFormat(
"pfs:", openString, (
const char*)&pfsFormatArg,
sizeof(pfsFormatArg));
448 printf(
">>> Failed to format new partition: %d\n", retVal);
455 return fsSizeMB - fsSizeLeft;
462 if(!hddStatusCurrent)
477 int fsSizeLeft = extraMB;
481 if(!hddStatusCurrent)
488 for(maxIndex = 0; maxIndex < 9; maxIndex++)
489 if((u32)(sizesMB[maxIndex]) == hddMaxPartitionSize)
493 for(useIndex = maxIndex; sizesMB[useIndex] > extraMB; useIndex--);
495 partSize = sizesMB[useIndex];
498 partFd = fileXioOpen(fs->
filename, FIO_O_RDWR);
505 if(fsSizeLeft < partSize)
508 printf(
">>> Adjusting sub size: %d MB to ", sizesMB[useIndex]);
510 for(useIndex = maxIndex; sizesMB[useIndex] > fsSizeLeft; useIndex--);
511 partSize = sizesMB[useIndex];
514 printf(
"%d MB\n", sizesMB[useIndex]);
520 printf(
">>> Attempting to create sub partition of size %d MB\n", sizesMB[useIndex]);
522 retVal = fileXioIoctl2(partFd, HIOCADDSUB, sizesString[useIndex], strlen(sizesString[useIndex]) + 1, NULL, 0);
523 if(retVal == -ENOSPC)
528 partSize = sizesMB[useIndex];
534 printf(
">>> Out of sizes to try. Giving up.\n");
539 printf(
">>> Subpartition alloc FAILED! Trying with size of %d MB\n", partSize);
545 else if(retVal == -EFBIG)
550 printf(
">>> Sub creation successfull!\n");
556 printf(
">>> Unknown error while creating sub: %d\n", retVal);
560 fsSizeLeft -= sizesMB[useIndex];
563 fileXioClose(partFd);
567 return extraMB - fsSizeLeft;