drivers/nvram_file/nvram_file.c
changeset 93 16c8ceea8f18
parent 92 0d84d95790d9
child 94 bdf4c86be6b2
equal deleted inserted replaced
92:0d84d95790d9 93:16c8ceea8f18
     1 #include <stdio.h>
       
     2 
       
     3 
       
     4 /* size of NVRAM in bytes */
       
     5 #define NVRAM_MAX_SIZE 262144
       
     6 
       
     7 /* size of block in byte */
       
     8 #define NVRAM_BLOCK_SIZE 256
       
     9 
       
    10 #define NVRAM_FILE_NAME "__nvram__"
       
    11 
       
    12 FILE *nvram_file = NULL;
       
    13 
       
    14 
       
    15 void iat_flash_read_regs();
       
    16 void iat_flash_write_regs();
       
    17 void iat_flash_write_page(unsigned int);
       
    18 void iat_flash_read_page(unsigned int);
       
    19 
       
    20 
       
    21 short data_len; /* 0 to NVRAM_BLOCK_SIZE bytes */
       
    22 short data_num_pages;
       
    23 unsigned int *data_page = NULL;
       
    24 unsigned int data_addr;
       
    25 
       
    26 unsigned int *regs_page = NULL;
       
    27 
       
    28 int iat_init()
       
    29 {
       
    30 	int i;
       
    31 
       
    32 	nvram_file = fopen(NVRAM_FILE_NAME, "wr");
       
    33 	if (nvram_file  == NULL)
       
    34 		return -1;
       
    35 
       
    36 	int n = NVRAM_BLOCK_SIZE / sizeof(unsigned int);
       
    37 
       
    38 	/* some actions to initialise the flash */
       
    39 	data_len = 0;
       
    40 	data_num_pages = 0;
       
    41 
       
    42 	data_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
       
    43 	memset(data_page, 0, sizeof(unsigned int)*n);
       
    44 
       
    45 	if (data_page == NULL)
       
    46 		return -1;
       
    47 
       
    48 	regs_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
       
    49 	memset(regs_page, 0, sizeof(unsigned int)*n);
       
    50 	if (regs_page == NULL)
       
    51 		return -2;
       
    52 
       
    53 	iat_flash_read_regs();
       
    54 
       
    55 	/* start the data at the location specified in the registers */ 
       
    56 	if (0) /* for now it is 0, but put here a test to know whether
       
    57                   or not the NVRAM has been written before */
       
    58 		data_addr = regs_page[1];
       
    59 	else
       
    60 		data_addr = NVRAM_BLOCK_SIZE; /* let start at block 1 */
       
    61 
       
    62 	/* create a file the size of the simulated NVRAM */
       
    63 	for(i=0; i<NVRAM_MAX_SIZE/NVRAM_BLOCK_SIZE + 1; i++)
       
    64 		fwrite(data_page, sizeof(unsigned int), n, nvram_file);
       
    65 
       
    66 	return 0;
       
    67 }
       
    68 
       
    69 
       
    70 void iat_end()
       
    71 {
       
    72 	iat_flash_write_page(data_addr);
       
    73 
       
    74 	iat_flash_write_regs();
       
    75 
       
    76 	fclose(nvram_file);
       
    77 }
       
    78 
       
    79  
       
    80 void iat_flash_read_regs()
       
    81 {
       
    82 	fseek(nvram_file, 0, SEEK_SET);
       
    83 	fread(regs_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
       
    84 }
       
    85 
       
    86 
       
    87 void iat_flash_write_regs()
       
    88 {
       
    89 	fseek(nvram_file, 0, SEEK_SET);
       
    90 	fwrite(regs_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
       
    91 }
       
    92 
       
    93 
       
    94 void iat_flash_read_page(unsigned int addr)
       
    95 {
       
    96 	fseek(nvram_file, addr*sizeof(unsigned int), SEEK_SET);
       
    97 	fread(data_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
       
    98 }
       
    99 
       
   100 
       
   101 void iat_flash_write_page(unsigned int addr)
       
   102 {
       
   103 	fseek(nvram_file, addr*sizeof(unsigned int), SEEK_SET);
       
   104 	fwrite(data_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
       
   105 }
       
   106 
       
   107 
       
   108 /*------------------------------------------------------*/
       
   109 int main()
       
   110 {
       
   111 	iat_init();
       
   112 	iat_end();
       
   113 }
       
   114 /*------------------------------------------------------*/