PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
part_driver.c
1#include "part_driver.h"
2#include <errno.h>
3#include <stdio.h>
4#include <string.h>
5#include <sysmem.h>
6
7#include <bdm.h>
8#include "mbr_types.h"
9#include "gpt_types.h"
10
11#include "module_debug.h"
12
13struct partition g_part[MAX_PARTITIONS];
14struct block_device g_part_bd[MAX_PARTITIONS];
15
16static struct file_system g_mbr_fs;
17static struct file_system g_gpt_fs;
18
19int GetNextFreePartitionIndex()
20{
21 // Loop and find the next free partition index.
22 for (int i = 0; i < MAX_PARTITIONS; i++)
23 {
24 if (g_part[i].bd == NULL)
25 return i;
26 }
27
28 // No more free partitions.
29 return -1;
30}
31
32//---------------------------------------------------------------------------
33void part_disconnect(struct block_device *bd)
34{
35 int i;
36
37 M_DEBUG("%s\n", __func__);
38
39 for (i = 0; i < MAX_PARTITIONS; ++i) {
40 if (g_part[i].bd == bd) {
41 bdm_disconnect_bd(&g_part_bd[i]);
42 g_part[i].bd = NULL;
43 }
44 }
45}
46
47//
48// Block device interface
49//
50static int part_read(struct block_device *bd, u64 sector, void *buffer, u16 count)
51{
52 struct partition *part = (struct partition *)bd->priv;
53
54 if ((part == NULL) || (part->bd == NULL))
55 return -1;
56
57#ifdef DEBUG
58 u64 finalSector = sector + bd->sectorOffset;
59 DEBUG_U64_2XU32(finalSector);
60 M_DEBUG("%s (%s %d %d) 0x%08x%08x %d\n", __func__, bd->name, bd->devNr, bd->parNr, finalSector_u32[1], finalSector_u32[0], count);
61#endif
62
63 return part->bd->read(part->bd, sector + bd->sectorOffset, buffer, count);
64}
65
66static int part_write(struct block_device *bd, u64 sector, const void *buffer, u16 count)
67{
68 struct partition *part = (struct partition *)bd->priv;
69
70 if ((part == NULL) || (part->bd == NULL))
71 return -1;
72
73#ifdef DEBUG
74 u64 finalSector = sector + bd->sectorOffset;
75 DEBUG_U64_2XU32(finalSector);
76 M_DEBUG("%s (%s %d %d) 0x%08x%08x %d\n", __func__, bd->name, bd->devNr, bd->parNr, finalSector_u32[1], finalSector_u32[0], count);
77#endif
78
79 return part->bd->write(part->bd, sector + bd->sectorOffset, buffer, count);
80}
81
82static void part_flush(struct block_device *bd)
83{
84 struct partition *part = (struct partition *)bd->priv;
85
86 M_DEBUG("%s\n", __func__);
87
88 if ((part == NULL) || (part->bd == NULL))
89 return;
90
91 return part->bd->flush(part->bd);
92}
93
94static int part_stop(struct block_device *bd)
95{
96 struct partition *part = (struct partition *)bd->priv;
97
98 M_DEBUG("%s\n", __func__);
99
100 if ((part == NULL) || (part->bd == NULL))
101 return -1;
102
103 return part->bd->stop(part->bd);
104}
105
106void part_init()
107{
108 int i;
109
110 M_DEBUG("%s\n", __func__);
111
112 for (i = 0; i < MAX_PARTITIONS; ++i) {
113 g_part[i].bd = NULL;
114
115 g_part_bd[i].priv = &g_part[i];
116 g_part_bd[i].read = part_read;
117 g_part_bd[i].write = part_write;
118 g_part_bd[i].flush = part_flush;
119 g_part_bd[i].stop = part_stop;
120 }
121
122 // Setup MBR file system driver:
123 g_mbr_fs.priv = NULL;
124 g_mbr_fs.name = "MBR";
125 g_mbr_fs.connect_bd = part_connect_mbr;
126 g_mbr_fs.disconnect_bd = part_disconnect;
127 bdm_connect_fs(&g_mbr_fs);
128
129 // Setup GPT file system driver:
130 g_gpt_fs.priv = NULL;
131 g_gpt_fs.name = "GPT";
132 g_gpt_fs.connect_bd = part_connect_gpt;
133 g_gpt_fs.disconnect_bd = part_disconnect;
134 bdm_connect_fs(&g_gpt_fs);
135}
u32 count
start sector of fragmented bd/file