Subversion Repositories freemyipod

Rev

Rev 785 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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