Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
770 user890104 1
//
2
//
3
//    Copyright 2011 user890104
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
 
26
#include "util.h"
27
 
28
 
29
const char* libusb_error_messages[13] =
30
{
31
    "No error",                /* LIBUSB_SUCCESS = 0               */
32
    "Input/output error",      /* LIBUSB_ERROR_IO = -1             */
33
    "Invalid parameter",       /* LIBUSB_ERROR_INVALID_PARAM = -2  */
34
    "Access denied",           /* LIBUSB_ERROR_ACCESS = -3         */
35
    "No such device",          /* LIBUSB_ERROR_NO_DEVICE = -4      */
36
    "Entity not found",        /* LIBUSB_ERROR_NOT_FOUND = -5      */
37
    "Resource busy",           /* LIBUSB_ERROR_BUSY = -6           */
38
    "Operation timed out",     /* LIBUSB_ERROR_TIMEOUT = -7        */
39
    "Overflow",                /* LIBUSB_ERROR_OVERFLOW = -8       */
40
    "Pipe error",              /* LIBUSB_ERROR_PIPE = -9           */
41
    "System call interrupted", /* LIBUSB_ERROR_INTERRUPTED = -10   */
42
    "Insufficient memory",     /* LIBUSB_ERROR_NO_MEM = -11        */
43
    "Operation not supported", /* LIBUSB_ERROR_NOT_SUPPORTED = -12 */
44
};
45
 
46
const char* emcore_error_messages[10] =
47
{
48
    "No error",               /* EMCORE_SUCCESS = 0               */
49
    "Invalid command",        /* EMCORE_ERROR_INVALID = 1         */
50
    "Command not supported",  /* EMCORE_ERROR_NOT_SUPPORTED = 2   */
51
    "Device is busy",         /* EMCORE_ERROR_BUSY = 3            */
52
    "No such device",         /* EMCORE_ERROR_NO_DEVICE = 4       */
53
    "Incomplete transfer",    /* EMCORE_ERROR_INCOMPLETE = 5      */
54
    "Overflow",               /* EMCORE_ERROR_OVERFLOW = 6        */
55
    "Not implemented",        /* EMCORE_ERROR_NOT_IMPLEMENTED = 7 */
56
    "No more entries in dir", /* EMCORE_ERROR_NO_MORE_ENTRIES = 8 */
57
    "I/O error",              /* EMCORE_ERROR_IO = 9              */
58
};
59
 
60
void dump_packet(const void* data, const size_t length)
61
{
62
    static size_t i;
63
 
64
    for (i = 0; i < length; ++i)
65
    {
66
        fprintf(stderr, "%02x ", *((uint8_t*)(data + i)));
67
 
68
        if (i % 4 == 3)
69
        {
70
            fprintf(stderr, " ");
71
        }
72
 
73
        if (i % 16 == 15 && i + 1 < length)
74
        {
75
            fprintf(stderr, "\n");
76
        }
77
    }
78
 
79
    fprintf(stderr, "\n");
80
}
81
 
82
void alignsplit(struct alignsizes* sizeptr, const uint32_t addr,
83
    const uint32_t size, const uint32_t blksize, const uint32_t align)
84
{
85
    uint32_t end, bodyaddr, tailaddr;
86
 
87
    if (size <= blksize)
88
    {
89
        sizeptr->head = size;
90
        sizeptr->body = 0;
91
        sizeptr->tail = 0;
92
 
93
        return;
94
    }
95
 
96
    end = addr + size;
97
 
98
    if (addr & (align -1))
99
    {
100
        bodyaddr = (addr + MIN(size, blksize)) & ~(align - 1);
101
    }
102
    else
103
    {
104
        bodyaddr = addr;
105
    }
106
 
107
    sizeptr->head = bodyaddr - addr;
108
 
109
    if ((size - sizeptr->head) & (align - 1))
110
    {
111
        tailaddr = ((end - MIN(end - bodyaddr, blksize) + align - 1) & ~(align - 1));
112
    }
113
    else
114
    {
115
        tailaddr = end;
116
    }
117
 
118
    sizeptr->tail = end - tailaddr;
119
    sizeptr->body = tailaddr - bodyaddr;
120
 
121
    return;
122
}
123
 
124
time_t fat_time_to_unix_ts(const short wrttime, const short wrtdate)
125
{
126
    struct tm result;
127
    /*
128
        Example time: 0x9365
129
        in memory:
130
        M M M S S S S S   H H H H H M M M
131
        as a number:
132
        H H H H H M M M   M M M S S S S S
133
 
134
        Example date: 0x3d37
135
        in memory:
136
        M M M D D D D D   Y Y Y Y Y Y Y M
137
        as a number:
138
        Y Y Y Y Y Y Y M   M M M D D D D D
139
    */
140
 
141
    result.tm_sec = ((wrttime & 0x1f) << 1);
142
    result.tm_min = (wrttime >> 5) & 0x3f;
143
    result.tm_hour = (wrttime >> 11) & 0x1f;
144
    result.tm_mday = wrtdate & 0x1f;
145
    result.tm_mon = ((wrtdate >> 5) & 0xf) - 1;
146
    result.tm_year = ((wrtdate >> 9) & 0x7f) + 80;
147
 
148
    return mktime(&result);
149
}
150
 
151
int32_t unix_ts_to_fat_time(const time_t datetime)
152
{
153
    struct tm* tm_ptr;
154
 
155
    tm_ptr = localtime(&datetime);
156
 
157
    return (tm_ptr->tm_sec >> 1) |
158
           (tm_ptr->tm_min << 5) |
159
           (tm_ptr->tm_hour << 0xb) |
160
           (tm_ptr->tm_mday << 0x10) |
161
           ((tm_ptr->tm_mon + 1) << 0x15) |
162
           ((tm_ptr->tm_year - 80) << 0x19);
163
}
164
 
165
void print_error(const int code)
166
{
167
    if (code > 0)
168
    {
169
        fprintf(stderr, "emcore error: %s\n", emcore_error_messages[code]);
170
    }
171
    else
172
    {
173
        fprintf(stderr, "libusb error: %s\n", (
174
            LIBUSB_ERROR_OTHER == code ?
175
                "Other error" :
176
                libusb_error_messages[-code]
177
        ));
178
    }
179
}