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