| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <time.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <sys/ioctl.h> | |
| #include <mtd/mtd-user.h> | |
| #include <errno.h> | |
| #include "gsw_oem_rw_interface.h" | |
| #define FLASH_DEVICE "/dev/mtd32" | |
| #define TEST_BLOCK_ID 1 // 测试使用的block ID | |
| void print_usage(void) | |
| { | |
| printf("Usage:\n"); | |
| printf(" 1: Write test data to block\n"); | |
| printf(" 2: Read data from block\n"); | |
| printf(" 0: Exit\n"); | |
| printf("Please input your choice: "); | |
| } | |
| int write_test(void) | |
| { | |
| int ret; | |
| struct mtd_info_user mtd_info; | |
| int fd; | |
| char *write_buf = NULL; | |
| unsigned int max_write_size; | |
| fd = open(FLASH_DEVICE, O_RDONLY); | |
| if (fd == -1) { | |
| printf("Error opening flash device: %s\n", strerror(errno)); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) { | |
| printf("Error getting MTD info: %s\n", strerror(errno)); | |
| close(fd); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| close(fd); | |
| max_write_size = mtd_info.erasesize; | |
| printf("MTD erase size: %u bytes\n", max_write_size); | |
| write_buf = (char *)malloc(max_write_size); | |
| if (write_buf == NULL) | |
| { | |
| printf("Failed to allocate write buffer\n"); | |
| return -1; | |
| } | |
| int block_id = -1; | |
| printf("enter block id \n"); | |
| ret = scanf("%d",&block_id); | |
| memset(write_buf, 0, max_write_size); | |
| snprintf(write_buf, max_write_size, "Test data for block %d, timestamp: %d", | |
| block_id, (int)time(NULL)); | |
| printf("Write data: %s\n", write_buf); | |
| printf("Writing data to block %d...\n", block_id); | |
| ret = gsw_oem_write_data_ext(block_id, write_buf, max_write_size); | |
| if (ret != GSW_HAL_SUCCESS) | |
| { | |
| printf("Write failed with error: %d\n", ret); | |
| } | |
| else | |
| { | |
| printf("Write successful\n"); | |
| } | |
| free(write_buf); | |
| return ret; | |
| } | |
| int read_test(void) | |
| { | |
| int ret; | |
| unsigned int read_len = 0; | |
| struct mtd_info_user mtd_info; | |
| int fd; | |
| char *read_buf = NULL; | |
| unsigned int max_read_size; | |
| fd = open(FLASH_DEVICE, O_RDONLY); | |
| if (fd == -1) { | |
| printf("Error opening flash device: %s\n", strerror(errno)); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) { | |
| printf("Error getting MTD info: %s\n", strerror(errno)); | |
| close(fd); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| close(fd); | |
| max_read_size = mtd_info.erasesize; | |
| printf("MTD erase size: %u bytes\n", max_read_size); | |
| read_buf = (char *)malloc(max_read_size); | |
| if (read_buf == NULL) | |
| { | |
| printf("Failed to allocate read buffer\n"); | |
| return -1; | |
| } | |
| memset(read_buf, 0, max_read_size); | |
| int block_id = -1; | |
| printf("enter block id \n"); | |
| ret = scanf("%d",&block_id); | |
| printf("Reading data from block %d...\n", block_id); | |
| ret = gsw_oem_read_data_ext(block_id, read_buf, &read_len); | |
| if (ret != GSW_HAL_SUCCESS) | |
| { | |
| printf("Read failed with error: %d\n", ret); | |
| } | |
| else | |
| { | |
| printf("Read %u bytes\n", read_len); | |
| printf("Read data: %s\n", read_buf); | |
| } | |
| free(read_buf); | |
| return ret; | |
| } | |
| int main() | |
| { | |
| int choice; | |
| int ret = 0; | |
| while (1) | |
| { | |
| print_usage(); | |
| if (scanf("%d", &choice) != 1) | |
| { | |
| printf("Invalid input\n"); | |
| while (getchar() != '\n'); | |
| continue; | |
| } | |
| while (getchar() != '\n'); | |
| switch (choice) | |
| { | |
| case 0: | |
| printf("Exit\n"); | |
| return 0; | |
| case 1: | |
| ret = write_test(); | |
| break; | |
| case 2: | |
| ret = read_test(); | |
| break; | |
| default: | |
| printf("Invalid choice\n"); | |
| break; | |
| } | |
| printf("\n"); | |
| } | |
| return ret; | |
| } | |