PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
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
25static void apaCalculateFreeSpace(u32 *free, u32 sectors)
26{
27 if(0x1FFFFF < sectors)
28 {
29 *free += sectors;
30 return;
31 }
32
33 if((*free & sectors) == 0)
34 {
35 *free |= sectors;
36 return;
37 }
38
39 for(sectors /= 2; 0x3FFFF < sectors; sectors /= 2)
40 *free |= sectors;
41}
42
43int apaGetFreeSectors(s32 device, u32 *free, apa_device_t *deviceinfo)
44{
45 u32 sectors, partMax;
46 int rv;
47 apa_cache_t *clink;
48
49 sectors = 0;
50 *free = 0;
51 if((clink = apaCacheGetHeader(device, APA_SECTOR_MBR, APA_IO_MODE_READ, &rv)) != NULL)
52 {
53 do{
54 if(clink->header->type == 0)
55 apaCalculateFreeSpace(free, clink->header->length);
56 sectors += clink->header->length;
57 }while((clink = apaGetNextHeader(clink, &rv)) != NULL);
58 }
59
60 if(rv == 0)
61 {
62 for(partMax = deviceinfo[device].partitionMaxSize; 0x0003FFFF < partMax; partMax = deviceinfo[device].partitionMaxSize)
63 { //As weird as it looks, this was how it was done in the original HDD.IRX.
64 for( ; 0x0003FFFF < partMax; partMax /= 2)
65 {
66 //Non-SONY: Perform 64-bit arithmetic here to avoid overflows when dealing with large disks.
67 if((sectors % partMax == 0) && ((u64)sectors + partMax < deviceinfo[device].totalLBA))
68 {
69 apaCalculateFreeSpace(free, partMax);
70 sectors += partMax;
71 break;
72 }
73 }
74
75 if(0x0003FFFF >= partMax)
76 break;
77 }
78
79 APA_PRINTF(APA_DRV_NAME": total = %08lx sectors, installable = %08lx sectors.\n", sectors, *free);
80 }
81
82 return rv;
83}