OpenCores
URL https://opencores.org/ocsvn/usb_nand_reader/usb_nand_reader/trunk

Subversion Repositories usb_nand_reader

[/] [usb_nand_reader/] [trunk/] [pc/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 pradd
#include <stdio.h>
2
#include <libusb-1.0/libusb.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <unistd.h>
6
 
7
#include "include/opcodes.h"
8
#include "include/actions.h"
9
#include "include/nand_vendors.h"
10
 
11
int main(int argc, char **argv)
12
{
13
        FILE* dump;
14
        libusb_context* ctx;
15
        libusb_device** list;
16
        libusb_device_handle* hDevice = NULL;
17
        struct libusb_device_descriptor descr;
18
        int                     count, i;
19
 
20
        unsigned char   bufferOut[64];
21
        unsigned char   status;
22
 
23
        pnand_t                 nand;
24
 
25
        libusb_init(&ctx);
26
 
27
        count = libusb_get_device_list(ctx, &list);
28
 
29
        if(0 == count)
30
        {
31
                fprintf(stderr, "Could not get device list\n");
32
                exit(-1);
33
        }
34
 
35
        for(i = 0; i < count; i++)
36
        {
37
                libusb_device* tmpDevice = list[i];
38
                libusb_get_device_descriptor(tmpDevice, &descr);
39
 
40
                if(0x1234 == descr.idVendor && 0x1 == descr.idProduct)
41
                {
42
                        libusb_open(tmpDevice, &hDevice);
43
                        break;
44
                }
45
        }
46
        libusb_free_device_list(list,1);
47
 
48
        if(NULL == hDevice)
49
        {
50
                fprintf(stderr, "Could not find the device\n");
51
                exit(-1);
52
        }
53
        else
54
        {
55
                printf("Device opened\n");
56
        }
57
 
58
 
59
        if(libusb_kernel_driver_active(hDevice, 0) == 1)
60
        {
61
                printf("Detaching driver\n");
62
                libusb_detach_kernel_driver(hDevice, 0);
63
        }
64
 
65
        libusb_claim_interface(hDevice, 0);
66
        //libusb_reset_device(hDevice);
67
 
68
        //==============================
69
        // USB initialization completed.
70
        //==============================
71
 
72
        nand = (pnand_t)malloc(sizeof(nand_t));
73
        memset(nand, 0, sizeof(nand_t));
74
 
75
        memset(bufferOut, 0, 64);
76
 
77
        nand_enable(hDevice, 0);
78
        nand_reset(hDevice);
79
        nand_read_id(hDevice, nand->id);
80
        if(nand_is_onfi(hDevice, nand->onfiSignature))
81
        {
82
                nand->onfiParameterPage = (unsigned char*)malloc(sizeof(unsigned char) * 256);
83
 
84
 
85
                nand_read_onfi_param_page(hDevice, nand->onfiParameterPage);
86
 
87
        }
88
 
89
        if(1 != get_nand_configuration(nand))
90
        {
91
                printf("Could not get configuration for this chip\n");
92
                nand_disble(hDevice);
93
                libusb_close(hDevice);
94
                libusb_exit(ctx);
95
                return -1;
96
        }
97
 
98
        // Set configuration data if chip is not ONFI compliant or has corrupted ONFI parameter page/signature
99
        if(NULL == nand->onfiParameterPage)
100
        {
101
                printf("Setting configuration data\n");
102
                nand_set_config_data(hDevice, nand);
103
        }
104
 
105
        print_nand_configuration(nand);
106
 
107
        /*
108
        printf("Status: %02x\n", nand_read_status(hDevice, NULL));
109
        nand_toggle_wp(hDevice);
110
 
111
        fprintf(stderr, "Erasing block\n");
112
        nand_block_erase(hDevice, 0);
113
        //sleep(1);
114
        printf("Status: %02x\n", nand_read_status_enhanced(hDevice, 0, NULL));
115
 
116
        fprintf(stderr, "Programming page\n");
117
 
118
        {
119
                int x;
120
                for(x = 0; x < nand->bytesPerPage + nand->oobPerPage; x++)
121
                {
122
                        nand->pageBuffer[x] = (unsigned char)((x) & 0xf0);
123
                }
124
        }
125
 
126
        nand_page_program(hDevice, 0, nand);
127
 
128
        printf("Status: %02x\n", nand_read_status_enhanced(hDevice, 0, NULL));
129
 
130
        nand_toggle_wp(hDevice);
131
        */
132
        printf("Dumping flash\n");
133
        i = 0;
134
        dump = fopen("flash_dump.bin", "wb");
135
 
136
        nand_read_status_enhanced(hDevice, 0, &status);
137
        printf("Status: %02x\n", status);
138
 
139
        /*
140
        for(count = 0; count < nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns; count++)
141
        {
142
                if(count % 64 == 0)
143
                {
144
                        printf("\rBlock #%d", i++);
145
                        fflush(stdout);
146
                }
147
                nand_read_page(hDevice, (unsigned int)count, nand);
148
                store_page(dump, nand);
149
        }
150
        */
151
        printf("Dumping %d pages\n", nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns);
152
        //nand_read_page_cache(hDevice, 0, nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns, dump, nand);
153
        nand_read_page_cache(hDevice, 0, 640, dump, nand);
154
 
155
        nand_read_status(hDevice, &status);
156
        printf("Status: %02x\n", status);
157
 
158
        fclose(dump);
159
        printf("\rdone.\n");
160
 
161
        free_nand_info(&nand);
162
 
163
        nand_disble(hDevice);
164
 
165
        //=================
166
        // Close USB stuff.
167
        //=================
168
 
169
        libusb_close(hDevice);
170
 
171
        libusb_exit(ctx);
172
 
173
        return 0;
174
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.