PS2SDK
PS2 Homebrew Libraries
free.c
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 # Free space calculation routines
11 */
12 
13 #include <errno.h>
14 #include <iomanX.h>
15 #ifdef _IOP
16 #include <sysclib.h>
17 #else
18 #include <string.h>
19 #endif
20 #include <stdio.h>
21 #include <hdd-ioctl.h>
22 
23 #include "libapa.h"
24 
25 static void apaCalculateFreeSpace(u32 *free, u32 sectors)
26 {
27  u32 maxsize = 0x1FFFFF; // 1GB
28  u32 minsize = 0x3FFFF; // 128MB
29 #ifdef APA_8MB_PARTITION_SIZE
30  minsize = 0x3FFF; // 8Mb
31 #endif
32  if (maxsize < sectors) {
33  *free += sectors;
34  return;
35  }
36 
37  if ((*free & sectors) == 0) {
38  *free |= sectors;
39  return;
40  }
41  for (sectors /= 2; minsize < sectors; sectors /= 2)
42  *free |= sectors;
43 }
44 
45 int apaGetFreeSectors(s32 device, u32 *free, apa_device_t *deviceinfo)
46 {
47  u32 sectors, partMax;
48  int rv;
49  apa_cache_t *clink;
50  u32 minsize = 0x3FFFF; // 128MB
51 #ifdef APA_8MB_PARTITION_SIZE
52  minsize = 0x3FFF; // 8Mb
53 #endif
54 
55  sectors = 0;
56  *free = 0;
57  if((clink = apaCacheGetHeader(device, APA_SECTOR_MBR, APA_IO_MODE_READ, &rv)) != NULL)
58  {
59  do{
60  if(clink->header->type == 0)
61  apaCalculateFreeSpace(free, clink->header->length);
62  sectors += clink->header->length;
63  }while((clink = apaGetNextHeader(clink, &rv)) != NULL);
64  }
65 
66  if(rv == 0)
67  {
68  for(partMax = deviceinfo[device].partitionMaxSize; minsize < partMax; partMax = deviceinfo[device].partitionMaxSize)
69  { //As weird as it looks, this was how it was done in the original HDD.IRX.
70  for( ; minsize < partMax; partMax /= 2)
71  {
72  //Non-SONY: Perform 64-bit arithmetic here to avoid overflows when dealing with large disks.
73  if((sectors % partMax == 0) && ((u64)sectors + partMax < deviceinfo[device].totalLBA))
74  {
75  apaCalculateFreeSpace(free, partMax);
76  sectors += partMax;
77  break;
78  }
79  }
80 
81  if(minsize >= partMax)
82  break;
83  }
84 
85  APA_PRINTF(APA_DRV_NAME": total = %08lx sectors, installable = %08lx sectors.\n", sectors, *free);
86  }
87 
88  return rv;
89 }
iomanX.h
sysclib.h
sapa_cache
Definition: libapa.h:120
hdd-ioctl.h
stdio.h
apa_device_t
Definition: libapa.h:209
errno.h