Date:2010-02-03 16:21:08 (10 years 3 months ago)
Author:Lars C.
Commit:f040ec232fc29df8e3e5e5425ad7c92160832285
Message:foobar

Files: usbboot/src/mem.c (1 diff)
usbboot/src/mem.h (1 diff)
usbboot/src/server.c (1 diff)

Change Details

usbboot/src/mem.c
1#include <stdint.h>
2#include <stdio.h>
3#include "ingenic_usb.h"
4
5#define MEM_WRITE 0
6#define MEM_READ 1
7
8#define MEM_8BIT (0 << 1)
9#define MEM_16BIT (1 << 1)
10#define MEM_32BIT (2 << 1)
11
12static void mem_write(struct ingenic_dev *dev, uint32_t addr, uint32_t val, unsigned int width)
13{
14    char ret[8];
15    usb_send_data_address_to_ingenic(dev, addr);
16    usb_send_data_length_to_ingenic(dev, val);
17    usb_ingenic_mem_ops(dev, MEM_WRITE | width);
18    usb_read_data_from_ingenic(dev, ret, ARRAY_SIZE(ret));
19}
20
21static uint32_t mem_read(struct ingenic_dev *dev, uint32_t addr, unsigned int width)
22{
23    char ret[8];
24    usb_send_data_address_to_ingenic(dev, addr);
25    usb_ingenic_mem_ops(dev, MEM_READ | width);
26    usb_read_data_from_ingenic(dev, ret, ARRAY_SIZE(ret));
27
28    return (((unsigned char)ret[3]) << 24) | (((unsigned char)ret[2]) << 16) |
29            (((unsigned char)ret[1]) << 8) | ((unsigned char)ret[0]);
30}
31
32void mem_write8(struct ingenic_dev *dev, uint32_t addr, uint8_t val)
33{
34    mem_write(dev, addr, val, MEM_8BIT);
35}
36
37void mem_write16(struct ingenic_dev *dev, uint32_t addr, uint16_t val)
38{
39    mem_write(dev, addr, val, MEM_16BIT);
40}
41
42void mem_write32(struct ingenic_dev *dev, uint32_t addr, uint32_t val)
43{
44    mem_write(dev, addr, val, MEM_32BIT);
45}
46
47uint8_t mem_read8(struct ingenic_dev *dev, uint32_t addr)
48{
49    uint8_t val = mem_read(dev, addr, MEM_8BIT) & 0xff;
50    return val;
51}
52
53uint16_t mem_read16(struct ingenic_dev *dev, uint32_t addr)
54{
55    return mem_read(dev, addr, MEM_16BIT) & 0xffff;
56}
57
58uint32_t mem_read32(struct ingenic_dev *dev, uint32_t addr)
59{
60    return mem_read(dev, addr, MEM_32BIT);
61}
usbboot/src/mem.h
1#ifndef __MEM_H
2#define __MEM_H
3
4struct ingenic_dev;
5
6void mem_write32(struct ingenic_dev *dev, uint32_t addr, uint32_t val);
7void mem_write16(struct ingenic_dev *dev, uint32_t addr, uint16_t val);
8void mem_write8(struct ingenic_dev *dev, uint32_t addr, uint8_t val);
9uint32_t mem_read32(struct ingenic_dev *dev, uint32_t addr);
10uint16_t mem_read16(struct ingenic_dev *dev, uint32_t addr);
11uint8_t mem_read8(struct ingenic_dev *dev, uint32_t addr);
12
13#endif
usbboot/src/server.c
1#include <sys/types.h>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include <arpa/inet.h>
5#include <string.h>
6#include <unistd.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10#include "mem.h"
11#include "cmd.h"
12#include "ingenic_usb.h"
13#include "ingenic_cfg.h"
14
15#define PORT 1338
16
17#define TYPE_READ8 0
18#define TYPE_READ16 1
19#define TYPE_READ32 2
20#define TYPE_WRITE8 3
21#define TYPE_WRITE16 4
22#define TYPE_WRITE32 5
23
24struct request {
25    uint32_t addr;
26    uint32_t value;
27    uint8_t type;
28};
29
30struct response {
31    uint32_t value;
32};
33
34#define CONFIG_FILE_PATH "/etc/xburst-tools/usbboot.cfg"
35
36struct ingenic_dev ingenic_dev;
37
38int main(int argc, char *argv[])
39{
40    int s, t;
41    unsigned int sinlen;
42    struct sockaddr_in sin;
43    struct request request;
44    struct response response;
45    char *cfgpath = CONFIG_FILE_PATH;
46
47    if (usb_ingenic_init(&ingenic_dev))
48         return EXIT_FAILURE;
49
50    if (parse_configure(&ingenic_dev.config, cfgpath))
51        return EXIT_FAILURE;
52
53    boot(&ingenic_dev, STAGE1_FILE_PATH, STAGE2_FILE_PATH);
54
55    if ( (s = socket(AF_INET, SOCK_STREAM, 0 ) ) < 0) {
56      return -1;
57    }
58
59    sin.sin_family = AF_INET; /*set protocol family to Internet */
60    sin.sin_port = htons(PORT); /* set port no. */
61    sin.sin_addr.s_addr = INADDR_ANY; /* set IP addr to any interface */
62
63    if (bind(s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ){
64         perror("bind"); return -1; /* bind error */
65    }
66
67    /* server indicates it's ready, max. listen queue is 5 */
68    if (listen(s, 5)) {
69       return -1;
70    }
71
72    sinlen = sizeof(sin);
73    if ( (t = accept(s, (struct sockaddr *) &sin, &sinlen) ) < 0 ){
74        perror("accept "); /* accpet error */
75        return -1;
76    }
77
78    while (1) {
79
80        if (recv(t, (char*)&request, sizeof(request), 0) < 0) {
81                perror("recv");
82                return -1;
83        }
84
85/* printf("Got request %x %x %x\n", request.type, request.addr,
86        request.value);*/
87
88        switch (request.type) {
89        case TYPE_READ8:
90            response.value = mem_read8(&ingenic_dev, request.addr);
91            break;
92        case TYPE_READ16:
93            response.value = mem_read16(&ingenic_dev, request.addr);
94            break;
95        case TYPE_READ32:
96            response.value = mem_read32(&ingenic_dev, request.addr);
97            break;
98        case TYPE_WRITE8:
99            mem_write8(&ingenic_dev, request.addr, request.value);
100            break;
101        case TYPE_WRITE16:
102            mem_write16(&ingenic_dev, request.addr, request.value);
103            break;
104        case TYPE_WRITE32:
105            mem_write32(&ingenic_dev, request.addr, request.value);
106            break;
107        }
108/* printf("Got response %x\n", response.value);*/
109
110        if (send(t, (char*)&response, sizeof(response),0 ) < 0 ) {
111            perror("send");
112            return -1;
113        }
114    }
115
116    close(t);
117    close(s);
118
119    return 0;
120}

Archive Download the corresponding diff file



interactive