sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
bd_defrag.c
1#include <bd_defrag.h>
2#include <stdio.h>
3
4//#define DEBUG //comment out this line when not debugging
5#include "module_debug.h"
6
7
8int bd_defrag_read(struct block_device* bd, u32 fragcount, struct bd_fragment* fraglist, u64 sector, void* buffer, u16 count)
9{
10 u64 sector_start = sector;
11 u16 count_left = count;
12
13 while (count_left > 0) {
14 u16 count_read;
15 u64 offset = 0; // offset of fragment in bd/file
16 struct bd_fragment *f = NULL;
17 int i;
18
19 // Locate fragment containing start sector
20 for (i=0; (u32)i<fragcount; i++) {
21 f = &fraglist[i];
22 if (offset <= sector_start && (offset + f->count) > sector_start) {
23 // Fragment found
24 break;
25 }
26 offset += f->count;
27 }
28
29 if ((u32)i == fragcount) {
30 M_PRINTF("%s: ERROR: fragment not found!\n", __FUNCTION__);
31 return -1;
32 }
33
34 // Clip to fragment size
35 count_read = count_left;
36 if ((sector_start + count_read) > (offset + f->count)) {
37 count_read = (offset + f->count) - sector_start;
38 M_DEBUG("%s: clipping sectors %d -> %d\n", __FUNCTION__, count_left, count_read);
39 }
40
41 // Do the read
42 if (bd->read(bd, f->sector + (sector_start - offset), buffer, count_read) != count_read) {
43 M_PRINTF("%s: ERROR: read failed!\n", __FUNCTION__);
44 return -1;
45 }
46
47 // Advance to next fragment
48 sector_start += count_read;
49 count_left -= count_read;
50 buffer = (u8*)buffer + (count_read * bd->sectorSize);
51 }
52
53 return count;
54}
55
56int bd_defrag_write(struct block_device* bd, u32 fragcount, struct bd_fragment* fraglist, u64 sector, const void* buffer, u16 count)
57{
58 u64 sector_start = sector;
59 u16 count_left = count;
60
61 while (count_left > 0) {
62 u16 count_write;
63 u64 offset = 0; // offset of fragment in bd/file
64 struct bd_fragment *f = NULL;
65 int i;
66
67 // Locate fragment containing start sector
68 for (i=0; (u32)i<fragcount; i++) {
69 f = &fraglist[i];
70 if (offset <= sector_start && (offset + f->count) > sector_start) {
71 // Fragment found
72 break;
73 }
74 offset += f->count;
75 }
76
77 if ((u32)i == fragcount) {
78 M_PRINTF("%s: ERROR: fragment not found!\n", __FUNCTION__);
79 return -1;
80 }
81
82 // Clip to fragment size
83 count_write = count_left;
84 if ((sector_start + count_write) > (offset + f->count)) {
85 count_write = (offset + f->count) - sector_start;
86 M_DEBUG("%s: clipping sectors %d -> %d\n", __FUNCTION__, count_left, count_write);
87 }
88
89 // Do the write
90 if (bd->write(bd, f->sector + (sector_start - offset), buffer, count_write) != count_write) {
91 M_PRINTF("%s: ERROR: write failed!\n", __FUNCTION__);
92 return -1;
93 }
94
95 // Advance to next fragment
96 sector_start += count_write;
97 count_left -= count_write;
98 buffer = (u8*)buffer + (count_write * bd->sectorSize);
99 }
100
101 return count;
102}
u32 count
start sector of fragmented bd/file
u32 count
start sector of fragmented bd/file