summaryrefslogtreecommitdiff
path: root/hwid_dec.c
blob: ba560b1f558dfadb62a2a23223fe30f6bd3bb963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

struct __attribute__((packed)) HWID {
    uint16_t size;
    uint16_t version;
    uint16_t instances[9];
    uint16_t dock_or_PCMCIA;
    uint16_t hashRAM;
    uint16_t hashBIOS;
    uint16_t instanceHashes[];
};

struct __attribute__((packed)) TIMEWEIGHT {
    uint8_t num_weights;
    struct __attribute__((packed)) Weight {
        uint8_t type;
        uint16_t weight;
    } weight[];
};

const char *CLASS_NAMES[] = {
    "CDROM",
    "Hard Disk Controllers",
    "Hard Disk Drives",
    "Displays",
    "SCSI Adapters",
    "PCMCIA",
    "Audio Adapters",
    "Dock",
    "Network Interface Cards",
    "CPUs",
    "Memory",
    "UNUSED",
    "BIOS",
    "UNUSED",
    "Mobile Broadband",
    "Bluetooth Interfaces"
};

const char *HWID_NAMES[] = {
    "CDROM / Mobile Broadband",
    "Hard Disk Controllers",
    "Hard Disk Drives",
    "Displays",
    "SCSI Adapters / Bluetooth Controllers",
    "Audio Adapters",
    "Unused",
    "Network Interface Cards",
    "CPUs"
};

void print_block(uint8_t *hw_block) {
    uint32_t size = *(uint32_t *)hw_block;
    uint16_t threshold = *(uint16_t *)(hw_block + 4);
    unsigned instance_count = 0;

    puts("Block Information:");
    printf("Size      : [%08x]\n", size);
    printf("Threshold : [%08x]\n", threshold);
    puts("");

    struct HWID *hwid = (struct HWID *)(hw_block + 6);
    puts("HWID Information:");
    printf("Size        : [%04x]\n", hwid->size);
    printf("Version     : [%04x]\n", hwid->version);
    for(size_t i = 0; i < 9; i++) {
        instance_count += hwid->instances[i];
        printf("Inst. (%02zx)  : [%04x] -> %s\n", i, hwid->instances[i], HWID_NAMES[i]);
    }
    printf("Dock/PCMCIA : [%04x]\n", hwid->dock_or_PCMCIA);
    printf("RAM hash    : [%02x]\n", hwid->hashRAM);
    printf("SMBIOS hash : [%04x]\n", hwid->hashBIOS);
    for(size_t i = 0; i < instance_count; i++) {
        printf("Hash (%02zx)   : [%04x] %s\n", i, hwid->instanceHashes[i], (hwid->instanceHashes[i] & 1) == 1 ? "[[Removable]]" : "");
    }
    puts("");

    struct TIMEWEIGHT *tw = (struct TIMEWEIGHT *) ((hw_block + 6) + hwid->size);
    puts("Timeweight Information:");
    printf("Weights     : [%02x]\n", tw->num_weights);

    for(size_t i = 0; i < tw->num_weights; i++) {
        printf("Weight (%02x) : [%04x] -> %s\n", tw->weight[i].type, tw->weight[i].weight, CLASS_NAMES[tw->weight[i].type]);
    }

}

int main(int argc, char *argv[]) {

    if(argc != 2) {
        puts("Invalid parameters.\nUsages:\n\thwid file");
        exit(1);
    }
    
    FILE *fd = fopen(argv[1], "r");
    fseek(fd, 0, SEEK_END);
    long fsize = ftell(fd);
    rewind(fd);

    uint8_t *hw_block = malloc(fsize);
    fread(hw_block, 1, fsize, fd);

    print_block(hw_block);

    return 0;
}