| 1 | //-------------------------------------------------------------- | 
 
 
 
 
 | 2 | //File name:   hdl.c | 
 
 
 
 
 | 3 | //-------------------------------------------------------------- | 
 
 
 
 
 | 4 | #include <thbase.h> | 
 
 
 
 
 | 5 | #include <stdio.h> | 
 
 
 
 
 | 6 | #include <sysclib.h> | 
 
 
 
 
 | 7 | #include <cdvdman.h> | 
 
 
 
 
 | 8 | #include <iomanX.h> | 
 
 
 
 
 | 9 | #include <sysmem.h> | 
 
 
 
 
 | 10 |  | 
 
 
 
 
 | 11 | #include "ps2_hdd.h" | 
 
 
 
 
 | 12 | #include "hdd.h" | 
 
 
 
 
 | 13 | #include "hdl.h" | 
 
 
 
 
 | 14 | #include "apa.h" | 
 
 
 
 
 | 15 |  | 
 
 
 
 
 | 16 | //-------------------------------------------------------------- | 
 
 
 
 
 | 17 | void hdl_glist_free (hdl_games_list_t *glist) | 
 
 
 
 
 | 18 | { | 
 
 
 
 
 | 19 | if (glist != NULL) | 
 
 
 
 
 | 20 | { | 
 
 
 
 
 | 21 | FreeSysMemory  (glist->games); | 
 
 
 
 
 | 22 | FreeSysMemory  (glist); | 
 
 
 
 
 | 23 | } | 
 
 
 
 
 | 24 | } | 
 
 
 
 
 | 25 | //------------------------------ | 
 
 
 
 
 | 26 | //endfunc hdl_glist_free | 
 
 
 
 
 | 27 | //-------------------------------------------------------------- | 
 
 
 
 
 | 28 | static int hdl_ginfo_read (hio_t *hio, const ps2_partition_header_t *part, hdl_game_info_t *ginfo) | 
 
 
 
 
 | 29 | { | 
 
 
 
 
 | 30 | u_long i, size; | 
 
 
 
 
 | 31 | /* data we're interested in starts @ 0x101000 and is header | 
 
 
 
 
 | 32 | * plus information for up to 65 partitions | 
 
 
 
 
 | 33 | * (1 main + 64 sub) by 12 bytes each */ | 
 
 
 
 
 | 34 | const u_long offset = 0x101000; | 
 
 
 
 
 | 35 | char buffer [1024]; | 
 
 
 
 
 | 36 | int result; | 
 
 
 
 
 | 37 | u_long bytes; | 
 
 
 
 
 | 38 |  | 
 
 
 
 
 | 39 | result = hio->read(hio, get_u32 (&part->start) + offset / 512, 2, buffer, &bytes); | 
 
 
 
 
 | 40 | if (result == 0) | 
 
 
 
 
 | 41 | { | 
 
 
 
 
 | 42 | if (bytes == 1024) | 
 
 
 
 
 | 43 | { | 
 
 
 
 
 | 44 | /* calculate total size */ | 
 
 
 
 
 | 45 | size = get_u32 (&part->length); | 
 
 
 
 
 | 46 | for (i=0; i<get_u32 (&part->nsub); ++i) | 
 
 
 
 
 | 47 | size += get_u32 (&part->subs [i].length); | 
 
 
 
 
 | 48 |  | 
 
 
 
 
 | 49 | memcpy (ginfo->partition_name, part->id, PS2_PART_IDMAX); | 
 
 
 
 
 | 50 | ginfo->partition_name [PS2_PART_IDMAX] = '\0'; | 
 
 
 
 
 | 51 | strcpy (ginfo->name, buffer + 8); | 
 
 
 
 
 | 52 | strcpy (ginfo->startup, buffer + 0xac); | 
 
 
 
 
 | 53 | ginfo->compat_flags = buffer [0xa8]; | 
 
 
 
 
 | 54 | ginfo->is_dvd = buffer [0xec] == 0x14; | 
 
 
 
 
 | 55 | ginfo->start_sector = get_u32 (&part->start); | 
 
 
 
 
 | 56 | ginfo->total_size_in_kb = size / 2; | 
 
 
 
 
 | 57 | } | 
 
 
 
 
 | 58 | else result = -1; | 
 
 
 
 
 | 59 | } | 
 
 
 
 
 | 60 | return (result); | 
 
 
 
 
 | 61 | } | 
 
 
 
 
 | 62 | //------------------------------ | 
 
 
 
 
 | 63 | //endfunc hdl_ginfo_read | 
 
 
 
 
 | 64 | //-------------------------------------------------------------- | 
 
 
 
 
 | 65 | int hdl_glist_read (hio_t *hio, hdl_games_list_t **glist) | 
 
 
 
 
 | 66 | { | 
 
 
 
 
 | 67 | apa_partition_table_t *ptable; | 
 
 
 
 
 | 68 | int result; | 
 
 
 
 
 | 69 |  | 
 
 
 
 
 | 70 | result = apa_ptable_read_ex (hio, &ptable); | 
 
 
 
 
 | 71 | if (result == 0){ | 
 
 
 
 
 | 72 | u_long i, count = 0; | 
 
 
 
 
 | 73 | void *tmp; | 
 
 
 
 
 | 74 | for (i=0; i<ptable->part_count; ++i) | 
 
 
 
 
 | 75 | count += (get_u16 (&ptable->parts [i].header.flags) == 0x00 && | 
 
 
 
 
 | 76 | get_u16 (&ptable->parts [i].header.type) == 0x1337); | 
 
 
 
 
 | 77 |  | 
 
 
 
 
 | 78 | tmp = AllocSysMemory(0, (sizeof (hdl_game_info_t) * count), NULL); | 
 
 
 
 
 | 79 | if (tmp != NULL){ | 
 
 
 
 
 | 80 | memset (tmp, 0, sizeof (hdl_game_info_t) * count); | 
 
 
 
 
 | 81 | *glist = AllocSysMemory(0, sizeof (hdl_games_list_t), NULL); | 
 
 
 
 
 | 82 | if (*glist != NULL){ | 
 
 
 
 
 | 83 | u_long index = 0; | 
 
 
 
 
 | 84 | memset (*glist, 0, sizeof (hdl_games_list_t)); | 
 
 
 
 
 | 85 | (*glist)->count = count; | 
 
 
 
 
 | 86 | (*glist)->games = tmp; | 
 
 
 
 
 | 87 | (*glist)->total_chunks = ptable->total_chunks; | 
 
 
 
 
 | 88 | (*glist)->free_chunks = ptable->free_chunks; | 
 
 
 
 
 | 89 | for (i=0; result==0&&i<ptable->part_count; ++i){ | 
 
 
 
 
 | 90 | const ps2_partition_header_t *part = &ptable->parts [i].header; | 
 
 
 
 
 | 91 | if (get_u16 (&part->flags) == 0x00 && get_u16 (&part->type) == 0x1337) | 
 
 
 
 
 | 92 | result = hdl_ginfo_read (hio, part, (*glist)->games + index++); | 
 
 
 
 
 | 93 | } | 
 
 
 
 
 | 94 | if (result != 0) | 
 
 
 
 
 | 95 | FreeSysMemory(*glist); | 
 
 
 
 
 | 96 | } else | 
 
 
 
 
 | 97 | result = -2; | 
 
 
 
 
 | 98 | if (result != 0) | 
 
 
 
 
 | 99 | FreeSysMemory(tmp); | 
 
 
 
 
 | 100 | } else result = -2; | 
 
 
 
 
 | 101 |  | 
 
 
 
 
 | 102 | apa_ptable_free (ptable); | 
 
 
 
 
 | 103 | } else { //apa_ptable_read_ex failed | 
 
 
 
 
 | 104 | } | 
 
 
 
 
 | 105 | return result; | 
 
 
 
 
 | 106 | } | 
 
 
 
 
 | 107 | //------------------------------ | 
 
 
 
 
 | 108 | //endfunc hdl_glist_read | 
 
 
 
 
 | 109 | //-------------------------------------------------------------- | 
 
 
 
 
 | 110 | static int hdl_ginfo_write (hio_t *hio, const ps2_partition_header_t *part, hdl_game_info_t *ginfo) | 
 
 
 
 
 | 111 | { | 
 
 
 
 
 | 112 | const u_long offset = 0x101000; | 
 
 
 
 
 | 113 | char buffer [1024]; | 
 
 
 
 
 | 114 | int result; | 
 
 
 
 
 | 115 | u_long bytes; | 
 
 
 
 
 | 116 |  | 
 
 
 
 
 | 117 | result = hio->read(hio, get_u32(&part->start) + offset / 512, 2, buffer, &bytes); | 
 
 
 
 
 | 118 |  | 
 
 
 
 
 | 119 | memset(buffer + 8, 0, PS2_PART_NAMEMAX); | 
 
 
 
 
 | 120 | memcpy(buffer + 8, ginfo->name, PS2_PART_NAMEMAX); | 
 
 
 
 
 | 121 |  | 
 
 
 
 
 | 122 | result = hio->write(hio, get_u32(&part->start) + offset / 512, 2, buffer, &bytes); | 
 
 
 
 
 | 123 |  | 
 
 
 
 
 | 124 | return result; | 
 
 
 
 
 | 125 | } | 
 
 
 
 
 | 126 | //------------------------------ | 
 
 
 
 
 | 127 | //endfunc hdl_ginfo_write | 
 
 
 
 
 | 128 | //-------------------------------------------------------------- | 
 
 
 
 
 | 129 | int hdl_glist_write (hio_t *hio, hdl_game_info_t *ginfo) | 
 
 
 
 
 | 130 | { | 
 
 
 
 
 | 131 | hdl_games_list_t *tmplist; | 
 
 
 
 
 | 132 | apa_partition_table_t *ptable; | 
 
 
 
 
 | 133 | int result; | 
 
 
 
 
 | 134 |  | 
 
 
 
 
 | 135 | result = apa_ptable_read_ex (hio, &ptable); | 
 
 
 
 
 | 136 | if (result == 0) | 
 
 
 
 
 | 137 | { | 
 
 
 
 
 | 138 | u_long i, count = 0; | 
 
 
 
 
 | 139 | void *tmp; | 
 
 
 
 
 | 140 | for (i=0; i<ptable->part_count; ++i) | 
 
 
 
 
 | 141 | count += (get_u16 (&ptable->parts [i].header.flags) == 0x00 && | 
 
 
 
 
 | 142 | get_u16 (&ptable->parts [i].header.type) == 0x1337); | 
 
 
 
 
 | 143 |  | 
 
 
 
 
 | 144 | tmp = AllocSysMemory(0, (sizeof (hdl_game_info_t) * count), NULL); | 
 
 
 
 
 | 145 | if (tmp != NULL) | 
 
 
 
 
 | 146 | { | 
 
 
 
 
 | 147 | memset (tmp, 0, sizeof (hdl_game_info_t) * count); | 
 
 
 
 
 | 148 | tmplist = AllocSysMemory(0, sizeof (hdl_games_list_t), NULL); | 
 
 
 
 
 | 149 | if (tmplist != NULL) | 
 
 
 
 
 | 150 | { | 
 
 
 
 
 | 151 | u_long index = 0; | 
 
 
 
 
 | 152 | memset (tmplist, 0, sizeof (hdl_games_list_t)); | 
 
 
 
 
 | 153 | tmplist->count = count; | 
 
 
 
 
 | 154 | tmplist->games = tmp; | 
 
 
 
 
 | 155 | tmplist->total_chunks = ptable->total_chunks; | 
 
 
 
 
 | 156 | tmplist->free_chunks = ptable->free_chunks; | 
 
 
 
 
 | 157 | for (i=0; result==0&&i<ptable->part_count; ++i) | 
 
 
 
 
 | 158 | { | 
 
 
 
 
 | 159 | const ps2_partition_header_t *part = &ptable->parts [i].header; | 
 
 
 
 
 | 160 | if (get_u16 (&part->flags) == 0x00 && get_u16 (&part->type) == 0x1337){ | 
 
 
 
 
 | 161 | result = hdl_ginfo_read(hio, part, tmplist->games + index++); | 
 
 
 
 
 | 162 | if(!strcmp(tmplist->games[index-1].partition_name, ginfo->partition_name)){ | 
 
 
 
 
 | 163 | result = hdl_ginfo_write(hio, part, ginfo); | 
 
 
 
 
 | 164 | break; | 
 
 
 
 
 | 165 | } | 
 
 
 
 
 | 166 | } | 
 
 
 
 
 | 167 | } | 
 
 
 
 
 | 168 | FreeSysMemory(tmplist); | 
 
 
 
 
 | 169 | } | 
 
 
 
 
 | 170 | else result = -2; | 
 
 
 
 
 | 171 | if (result != 0) FreeSysMemory(tmp); | 
 
 
 
 
 | 172 | } | 
 
 
 
 
 | 173 | else result = -2; | 
 
 
 
 
 | 174 |  | 
 
 
 
 
 | 175 | apa_ptable_free (ptable); | 
 
 
 
 
 | 176 | } | 
 
 
 
 
 | 177 | return result; | 
 
 
 
 
 | 178 | } | 
 
 
 
 
 | 179 | //------------------------------ | 
 
 
 
 
 | 180 | //endfunc hdl_glist_write | 
 
 
 
 
 | 181 | //-------------------------------------------------------------- | 
 
 
 
 
 | 182 | //End of file: hdl.c | 
 
 
 
 
 | 183 | //-------------------------------------------------------------- |