yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2016, ZIXC Corporation. |
| 3 | * |
| 4 | * File Name:cmd_compat_read.c |
| 5 | * File Mark: |
| 6 | * Description: |
| 7 | * Others: |
| 8 | * Version: 1.0 |
| 9 | * Author: zangxiaofeng |
| 10 | * Date: 2013-3-13 |
| 11 | * History 1: |
| 12 | * Date: |
| 13 | * Version: |
| 14 | * Author: |
| 15 | * Modification: |
| 16 | * History 2: |
| 17 | ********************************************************************************/ |
| 18 | |
| 19 | |
| 20 | /**************************************************************************** |
| 21 | * Include files |
| 22 | ****************************************************************************/ |
| 23 | #include <common.h> |
| 24 | #include <command.h> |
| 25 | #include <net.h> |
| 26 | #include <jffs2/load_kernel.h> |
| 27 | #include "downloader_config.h" |
| 28 | #include "downloader_nand.h" |
| 29 | #include "downloader_serial.h" |
| 30 | |
| 31 | #include "errno.h" |
| 32 | |
| 33 | extern int downloader_readline (char * buffer); |
| 34 | extern char *tsp_console_buffer; |
| 35 | |
| 36 | /******************************************************************************* |
| 37 | * Function:do_yaffs_write |
| 38 | * Description: |
| 39 | * Parameters: |
| 40 | * Input: |
| 41 | * |
| 42 | * Output: |
| 43 | * |
| 44 | * Returns: |
| 45 | * |
| 46 | * |
| 47 | * Others: |
| 48 | ********************************************************************************/ |
| 49 | int do_yaffs_write(partition_entry_t *part, char *par , unsigned int offset, unsigned int size ) |
| 50 | { |
| 51 | char *ack = tsp_console_buffer; |
| 52 | unsigned int offset_par = offset; |
| 53 | unsigned int leftWriteLength = 0; |
| 54 | unsigned int curWriteLength = 0; |
| 55 | unsigned int size_fs = 0; |
| 56 | unsigned int pagefullsize = 0; |
| 57 | #ifdef CONFIG_LOAD_CRC |
| 58 | unsigned long crc = 0; |
| 59 | unsigned long crc1 = 0; |
| 60 | #endif |
| 61 | int ret = 0; |
| 62 | nand_info_t *pNandInfo = NULL; |
| 63 | |
| 64 | if(part == NULL) |
| 65 | { |
| 66 | sprintf(ack,"FAIL INVALID PARTITION"); |
| 67 | downloader_serial_write(ack, strlen(ack)+1); |
| 68 | return EINVAL; /* Invalid argument */ |
| 69 | } |
| 70 | pNandInfo = &nand_info[nand_curr_device]; |
| 71 | assert(pNandInfo!=NULL); |
| 72 | pagefullsize=pNandInfo->writesize+pNandInfo->oobsize; |
| 73 | if((size==0)||(size>(part->part_size/pNandInfo->writesize)*pagefullsize)) |
| 74 | { |
| 75 | sprintf(ack,"FAIL INVALID LENGTH"); |
| 76 | downloader_serial_write(ack, strlen(ack)+1); |
| 77 | return EINVAL; /* Invalid argument */ |
| 78 | } |
| 79 | |
| 80 | leftWriteLength = size; |
| 81 | ret = downloader_nand_erase(part,part->part_size); |
| 82 | if(ret) |
| 83 | { |
| 84 | sprintf(ack,"FAIL ERASE ERROR"); |
| 85 | downloader_serial_write(ack, strlen(ack)+1); |
| 86 | return ENOSYS; /* Function not implemented */ |
| 87 | } |
| 88 | |
| 89 | while(leftWriteLength>0) |
| 90 | { |
| 91 | curWriteLength = MIN(leftWriteLength,DOWNLOADER_BUFFER_SIZE); |
| 92 | #ifdef CONFIG_LOAD_CRC |
| 93 | sprintf(ack,"DATACRC %08x",curWriteLength); |
| 94 | #else |
| 95 | sprintf(ack,"DATA %08x",curWriteLength); |
| 96 | #endif |
| 97 | downloader_serial_write(ack, strlen(ack)+1); |
| 98 | #ifdef CONFIG_LOAD_CRC |
| 99 | downloader_serial_read_actuallen((char *)DOWNLOADER_BUFFER_BASE, curWriteLength+4); |
| 100 | crc = crc32(0,(unsigned char*)DOWNLOADER_BUFFER_BASE,curWriteLength); |
| 101 | memcpy((unsigned char *)(&crc1),(unsigned char*)(DOWNLOADER_BUFFER_BASE+curWriteLength),4); |
| 102 | if(crc != crc1) |
| 103 | { |
| 104 | sprintf(ack,"FAIL CRC ERROR"); |
| 105 | downloader_serial_write(ack, strlen(ack)+1); |
| 106 | return ENOSYS; /* Function not implemented */ |
| 107 | } |
| 108 | #else |
| 109 | downloader_serial_read_actuallen((char *)DOWNLOADER_BUFFER_BASE, curWriteLength); |
| 110 | #endif |
| 111 | size_fs = (curWriteLength%pagefullsize)?(curWriteLength/pagefullsize+1)*(pagefullsize): curWriteLength; |
| 112 | if(curWriteLength<size_fs) |
| 113 | { |
| 114 | memset((char *)(DOWNLOADER_BUFFER_BASE+curWriteLength),0xff,size_fs-curWriteLength); |
| 115 | } |
| 116 | ret = downloader_nand_fs_write( part, offset_par, size_fs, (unsigned char *)DOWNLOADER_BUFFER_BASE); |
| 117 | if(ret) |
| 118 | { |
| 119 | sprintf(ack,"FAIL YAFFS WRITE %d,%d",offset_par,size_fs); |
| 120 | downloader_serial_write(ack, strlen(ack)+1); |
| 121 | return ENOSYS; /* Function not implemented */ |
| 122 | } |
| 123 | leftWriteLength -= curWriteLength; |
| 124 | offset_par += (curWriteLength/pagefullsize)*(pNandInfo->writesize); |
| 125 | } |
| 126 | |
| 127 | sprintf(ack,"OKAY"); |
| 128 | downloader_serial_write(ack, strlen(ack)+1); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | /******************************************************************************* |
| 135 | * Function:do_yaffs_read |
| 136 | * Description: |
| 137 | * Parameters: |
| 138 | * Input: |
| 139 | * |
| 140 | * Output: |
| 141 | * |
| 142 | * Returns: |
| 143 | * |
| 144 | * |
| 145 | * Others: |
| 146 | ********************************************************************************/ |
| 147 | int do_yaffs_read(partition_entry_t *part, char *par , unsigned int offset, unsigned int size) |
| 148 | { |
| 149 | char *rx_buffer = tsp_console_buffer; |
| 150 | char *ack = tsp_console_buffer; |
| 151 | unsigned int offset_par = offset; |
| 152 | unsigned int size_fs = 0; |
| 153 | unsigned int pagefullsize = 0; |
| 154 | nand_info_t *pNandInfo = NULL; |
| 155 | unsigned int leftReadLength = 0; |
| 156 | unsigned int curReadLength = 0; |
| 157 | //unsigned long crc = 0; |
| 158 | int ret = 0; |
| 159 | |
| 160 | if(part == NULL) |
| 161 | { |
| 162 | sprintf(ack,"FAIL INVALID PARTITION"); |
| 163 | downloader_serial_write(ack, strlen(ack)+1); |
| 164 | return EINVAL; /* Invalid argument */ |
| 165 | } |
| 166 | if((size == 0)||(size > part->part_size)) |
| 167 | { |
| 168 | sprintf(ack,"FAIL INVALID LENGTH"); |
| 169 | downloader_serial_write(ack, strlen(ack)+1); |
| 170 | return EINVAL; /* Invalid argument */ |
| 171 | } |
| 172 | pNandInfo = &nand_info[nand_curr_device]; |
| 173 | assert(pNandInfo!=NULL); |
| 174 | pagefullsize = pNandInfo->writesize+pNandInfo->oobsize; |
| 175 | leftReadLength = size; |
| 176 | |
| 177 | sprintf(ack,"DATA %08x",MIN(size,DOWNLOADER_BUFFER_SIZE)); |
| 178 | downloader_serial_write(ack, strlen(ack)+1); |
| 179 | |
| 180 | while(leftReadLength>0) |
| 181 | { |
| 182 | curReadLength = MIN(leftReadLength,DOWNLOADER_BUFFER_SIZE); |
| 183 | size_fs = (curReadLength%pagefullsize)?(curReadLength/pagefullsize+1)*(pagefullsize): curReadLength; |
| 184 | downloader_readline(rx_buffer); |
| 185 | if(memcmp(rx_buffer,"OKAY",4)==0) |
| 186 | { |
| 187 | ret = downloader_nand_fs_read( part, offset_par, size_fs, (unsigned char *)DOWNLOADER_BUFFER_BASE); |
| 188 | if(ret) |
| 189 | { |
| 190 | sprintf(ack,"FAIL YAFFS READ %d,%d, ret = %d",offset_par,size_fs,ret); |
| 191 | downloader_serial_write(ack, strlen(ack)+1); |
| 192 | return ENOSYS; /* Function not implemented */ |
| 193 | } |
| 194 | downloader_serial_write_actuallen((const char *)DOWNLOADER_BUFFER_BASE, curReadLength); |
| 195 | |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | sprintf(ack,"FAIL COMMAND ERROR"); |
| 200 | downloader_serial_write(ack, strlen(ack)+1); |
| 201 | return EBADRQC; /* Invalid request code */ |
| 202 | } |
| 203 | leftReadLength -= curReadLength; |
| 204 | offset_par += (curReadLength/pagefullsize)*(pNandInfo->writesize); |
| 205 | } |
| 206 | printf("prepare to receive OKAY"); |
| 207 | downloader_readline(rx_buffer); |
| 208 | if(memcmp(rx_buffer,"OKAY",4)==0) |
| 209 | { |
| 210 | return 0; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | sprintf(ack,"FAIL COMMAND ERROR"); |
| 215 | downloader_serial_write(ack, strlen(ack)+1); |
| 216 | return EBADRQC; /* Invalid request code */ |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |