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] - Rev 6

Go to most recent revision | Compare with Previous | Blame | View Log

#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#include "include/opcodes.h"
#include "include/actions.h"
#include "include/nand_vendors.h"
 
int main(int argc, char **argv)
{
	FILE* dump;
	libusb_context* ctx;
	libusb_device** list;
	libusb_device_handle* hDevice = NULL;
	struct libusb_device_descriptor descr;
	int 			count, i;
 
	unsigned char	bufferOut[64];
	unsigned char 	status;
 
	pnand_t			nand;
 
	libusb_init(&ctx);
 
	count = libusb_get_device_list(ctx, &list);
 
	if(0 == count)
	{
		fprintf(stderr, "Could not get device list\n");
		exit(-1);
	}
 
	for(i = 0; i < count; i++)
	{
		libusb_device* tmpDevice = list[i];
		libusb_get_device_descriptor(tmpDevice, &descr);
 
		if(0x1234 == descr.idVendor && 0x1 == descr.idProduct)
		{
			libusb_open(tmpDevice, &hDevice);
			break;
		}
	}
	libusb_free_device_list(list,1);
 
	if(NULL == hDevice)
	{
		fprintf(stderr, "Could not find the device\n");
		exit(-1);
	}
	else
	{
		printf("Device opened\n");
	}
 
 
	if(libusb_kernel_driver_active(hDevice, 0) == 1)
	{
		printf("Detaching driver\n");
		libusb_detach_kernel_driver(hDevice, 0);
	}
 
	libusb_claim_interface(hDevice, 0);
	//libusb_reset_device(hDevice);
 
	//==============================
	// USB initialization completed.
	//==============================
 
	nand = (pnand_t)malloc(sizeof(nand_t));
	memset(nand, 0, sizeof(nand_t));
 
	memset(bufferOut, 0, 64);
 
	nand_enable(hDevice, 0);
	nand_reset(hDevice);
	nand_read_id(hDevice, nand->id);
	if(nand_is_onfi(hDevice, nand->onfiSignature))
	{
		nand->onfiParameterPage = (unsigned char*)malloc(sizeof(unsigned char) * 256);
 
 
		nand_read_onfi_param_page(hDevice, nand->onfiParameterPage);
 
	}
 
	if(1 != get_nand_configuration(nand))
	{
		printf("Could not get configuration for this chip\n");
		nand_disble(hDevice);
		libusb_close(hDevice);
		libusb_exit(ctx);
		return -1;
	}
 
	// Set configuration data if chip is not ONFI compliant or has corrupted ONFI parameter page/signature
	if(NULL == nand->onfiParameterPage)
	{
		printf("Setting configuration data\n");
		nand_set_config_data(hDevice, nand);
	}
 
	print_nand_configuration(nand);
 
	/*
	printf("Status: %02x\n", nand_read_status(hDevice, NULL));
	nand_toggle_wp(hDevice);
 
	fprintf(stderr, "Erasing block\n");
	nand_block_erase(hDevice, 0);
	//sleep(1);
	printf("Status: %02x\n", nand_read_status_enhanced(hDevice, 0, NULL));
 
	fprintf(stderr, "Programming page\n");
 
	{
		int x;
		for(x = 0; x < nand->bytesPerPage + nand->oobPerPage; x++)
		{
			nand->pageBuffer[x] = (unsigned char)((x) & 0xf0);
		}
	}
 
	nand_page_program(hDevice, 0, nand);
 
	printf("Status: %02x\n", nand_read_status_enhanced(hDevice, 0, NULL));
 
	nand_toggle_wp(hDevice);
	*/
	printf("Dumping flash\n");
	i = 0;
	dump = fopen("flash_dump.bin", "wb");
 
	nand_read_status_enhanced(hDevice, 0, &status);
	printf("Status: %02x\n", status);
 
	/*
	for(count = 0; count < nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns; count++)
	{
		if(count % 64 == 0)
		{
			printf("\rBlock #%d", i++);
			fflush(stdout);
		}
		nand_read_page(hDevice, (unsigned int)count, nand);
		store_page(dump, nand);
	}
	*/
	printf("Dumping %d pages\n", nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns);
	//nand_read_page_cache(hDevice, 0, nand->pagesPerBlock * nand->blocksPerPlane * nand->planesPerLun * nand->numLuns, dump, nand);
	nand_read_page_cache(hDevice, 0, 640, dump, nand);
 
	nand_read_status(hDevice, &status);
	printf("Status: %02x\n", status);
 
	fclose(dump);
	printf("\rdone.\n");
 
	free_nand_info(&nand);
 
	nand_disble(hDevice);
 
	//=================
	// Close USB stuff.
	//=================
 
	libusb_close(hDevice);
 
	libusb_exit(ctx);
 
	return 0;
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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