#include "kernel.h" #undef __FILENAME__ #define __FILENAME__ "kernel.c" // 设备内核 extern const unsigned char Image$$KERNEL_ROM$$Base; extern char Image$$KERNEL_ROM$$Length[]; #define KERNEL_DEVICE_COUNT (((int)Image$$KERNEL_ROM$$Length) >> 2) static int *kernel_map = (int *)(&Image$$KERNEL_ROM$$Base); #define kernel_list(x) ((KENERAL_NODE *)(kernel_map[x])) int open(char *filename, int flags) { int i = 0; int status = K_FAIL; for (i = 0; i < KERNEL_DEVICE_COUNT; i++) { if (strcmp(filename, kernel_list(i)->filename) == 0) { if (kernel_list(i)->ops->f_open == NULL) { return K_OPS_NULL; } status = kernel_list(i)->ops->f_open(flags); if (status == K_SUCCESS) { return i; } else { return k_OPEN_FAIL; } } } return K_NOFOUND; } int write(int handle, unsigned char *buff, unsigned int len, void *param) { int status = K_FAIL; if ((handle < KERNEL_DEVICE_COUNT) && (handle >= 0)) { if (kernel_list(handle)->ops->f_write == NULL) { return K_OPS_NULL; } status = kernel_list(handle)->ops->f_write(buff, len, param); return status; } return K_FAIL; } int read(int handle, unsigned char *buff, unsigned int len, void *param) { int status = K_FAIL; if ((handle < KERNEL_DEVICE_COUNT) && (handle >= 0)) { if (kernel_list(handle)->ops->f_read == NULL) { return K_OPS_NULL; } status = kernel_list(handle)->ops->f_read(buff, len, param); return status; } return K_FAIL; } int ioctl(int handle, unsigned int cmd, unsigned long arg) { int status = K_FAIL; if ((handle < KERNEL_DEVICE_COUNT) && (handle >= 0)) { if (kernel_list(handle)->ops->f_ioctl == NULL) { return K_OPS_NULL; } status = kernel_list(handle)->ops->f_ioctl(cmd, arg); return status; } return K_FAIL; } int close(int handle) { int status = K_FAIL; if ((handle < KERNEL_DEVICE_COUNT) && (handle >= 0)) { if (kernel_list(handle)->ops->f_close == NULL) { return K_OPS_NULL; } status = kernel_list(handle)->ops->f_close(); return status; } return K_FAIL; } int isopen(int handle) { int status = K_FAIL; if ((handle < KERNEL_DEVICE_COUNT) && (handle >= 0)) { if (kernel_list(handle)->ops->f_isopen == NULL) { return K_OPS_NULL; } status = kernel_list(handle)->ops->f_isopen(); return status; } return K_FAIL; }