summaryrefslogtreecommitdiffstats
path: root/utils/imxtools/sbtools/rsrc.h
blob: 9dfd27b4659084c256bb1308a6103c016c1cb95f (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2012 Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef __RSRC_H__
#define __RSRC_H__

#include <stdint.h>
#include <stdbool.h>

#include "misc.h"

/**
 * Low-Level
 **/

#define RSRC_SECTOR_SIZE    2048

#define RSRC_TABLE_ENTRY_TYPE(e)    ((e) >> 28)
#define RSRC_TABLE_ENTRY_OFFSET(e)  ((e) & 0xfffffff)

#define RSRC_TYPE_NONE      0 /* empty entry */
#define RSRC_TYPE_NESTED    1 /* nested entry: points to a sub-table */
#define RSRC_TYPE_IMAGE     2 /* image entry */
#define RSRC_TYPE_VALUE     3 /* value stored on 28-bits */
#define RSRC_TYPE_AUDIO     4 /* audio entry */
#define RSRC_TYPE_DATA      5 /* data entry */

/**
 * API
 **/

struct rsrc_entry_t
{
    uint32_t id;
    uint32_t offset; // contains value of RSRC_TYPE_VALUE
    int size;
};

struct rsrc_file_t
{
    void *data;
    int size;

    int nr_entries;
    int capacity;
    struct rsrc_entry_t *entries;
};

enum rsrc_error_t
{
    RSRC_SUCCESS = 0,
    RSRC_ERROR = -1,
    RSRC_OPEN_ERROR = -2,
    RSRC_READ_ERROR = -3,
    RSRC_WRITE_ERROR = -4,
    RSRC_FORMAT_ERROR = -5,
    RSRC_CHECKSUM_ERROR = -6,
    RSRC_NO_VALID_KEY = -7,
    RSRC_CRYPTO_ERROR = -8,
};

enum rsrc_error_t rsrc_write_file(struct rsrc_file_t *rsrc, const char *filename);

typedef void (*rsrc_color_printf)(void *u, bool err, color_t c, const char *f, ...);
struct rsrc_file_t *rsrc_read_file(const char *filename, void *u,
    rsrc_color_printf printf, enum rsrc_error_t *err);
/* use size_t(-1) to use maximum size */
struct rsrc_file_t *rsrc_read_file_ex(const char *filename, size_t offset, size_t size, void *u,
    rsrc_color_printf printf, enum rsrc_error_t *err);
struct rsrc_file_t *rsrc_read_memory(void *buffer, size_t size, void *u,
    rsrc_color_printf printf, enum rsrc_error_t *err);

void rsrc_dump(struct rsrc_file_t *file, void *u, rsrc_color_printf printf);
void rsrc_free(struct rsrc_file_t *file);

#endif /* __RSRC_H__ */