]> FriiDump Source - friidump.git/blob - libfriidump/nintendo_disc_header.c
FriiDump 0.5.3.16 candidate: add native reports and Linux Xbox support
[friidump.git] / libfriidump / nintendo_disc_header.c
1 /***************************************************************************
2  *   Nintendo GameCube/Wii disc-header classifier                         *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  ***************************************************************************/
9
10 #include "nintendo_disc_header.h"
11
12 #define NINTENDO_WII_MAGIC 0x5d1c9ea3u
13 #define NINTENDO_GAMECUBE_MAGIC 0xc2339f3du
14
15 static uint32_t nintendo_read_be32(const uint8_t *data) {
16     return ((uint32_t)data[0] << 24) |
17            ((uint32_t)data[1] << 16) |
18            ((uint32_t)data[2] << 8) |
19            (uint32_t)data[3];
20 }
21
22 nintendo_disc_header_type nintendo_disc_header_detect(
23     const uint8_t *sector,
24     size_t sector_length
25 ) {
26     if (!sector || sector_length < 0x20)
27         return NINTENDO_DISC_HEADER_UNKNOWN;
28
29     if (nintendo_read_be32(sector + 0x18) == NINTENDO_WII_MAGIC)
30         return NINTENDO_DISC_HEADER_WII;
31
32     if (nintendo_read_be32(sector + 0x1c) == NINTENDO_GAMECUBE_MAGIC)
33         return NINTENDO_DISC_HEADER_GAMECUBE;
34
35     return NINTENDO_DISC_HEADER_UNKNOWN;
36 }
37
38 const char *nintendo_disc_header_type_name(
39     nintendo_disc_header_type type
40 ) {
41     switch (type) {
42         case NINTENDO_DISC_HEADER_GAMECUBE:
43             return "GameCube";
44         case NINTENDO_DISC_HEADER_WII:
45             return "Wii";
46         default:
47             return "Unknown";
48     }
49 }