From 579a8d374c07e90732edf2be33ca0a3f75aad346 Mon Sep 17 00:00:00 2001 From: Roman Hargrave Date: Mon, 26 Sep 2016 23:01:34 -0500 Subject: [PATCH] Finish cleanup --- amifldrv.c | 141 +++++++++++++++++++++++-------------- amifldrv.h | 13 ---- amiwrap.c | 191 -------------------------------------------------- amiwrap.h | 21 ------ linux_iface.h | 11 --- 5 files changed, 90 insertions(+), 287 deletions(-) delete mode 100644 amifldrv.h delete mode 100644 amiwrap.c delete mode 100644 amiwrap.h delete mode 100644 linux_iface.h diff --git a/amifldrv.c b/amifldrv.c index b9dd274..9fd5e61 100644 --- a/amifldrv.c +++ b/amifldrv.c @@ -11,16 +11,40 @@ #include #include #include +#include -#include "amifldrv.h" -#include "linux_iface.h" +#define LINUX_PRE_2_6 (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) +#define LINUX_POST_2_6 (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) + +#if LINUX_PRE_2_6 +# include +#else +# define mem_map_reserve(p) set_bit (PG_reserved, &((p)->flags)) +# define mem_map_unreserve(p) clear_bit(PG_reserved, &((p)->flags)) +#endif + +static int const CMD_ALLOC = 0x4160; +static int const CMD_FREE = 0x4161; +static int const CMD_LOCK_KB = 0x4162; +static int const CMD_UNLOCK_KB = 0x4163; + +/* + * ioctl data packet used to communicate instructions to the driver + */ +typedef struct _struct_AMIFL_alloc_params { + long size; + unsigned long kvirtlen; + void* kmallocptr; + void* kvirtadd; + void* kphysadd; +} AMIFL_alloc_params; static int *kmalloc_area = NULL; static int *kmalloc_ptr = NULL; static unsigned long kmalloc_len = 0L; static int kcount = 0; static int major; -static AMIFLDRV_ALLOC kmalloc_drv[128]; +static AMIFL_alloc_params kmalloc_drv[128]; /* * Section: Character Device Implementation @@ -77,9 +101,8 @@ AMI_chrdrv_ioctl(struct inode* _unused_inode, unsigned int cmd, unsigned long ar { case CMD_ALLOC: { - int i; - unsigned long virt_addr; - AMIFLDRV_ALLOC arg_kernel_space; + unsigned long virt_addr; + AMIFL_alloc_params arg_kernel_space; if (kcount >= 128) { @@ -93,44 +116,52 @@ AMI_chrdrv_ioctl(struct inode* _unused_inode, unsigned int cmd, unsigned long ar return -EINVAL; } - copy_from_user((void*) &arg_kernel_space, (void*) arg, sizeof(AMIFLDRV_ALLOC)); + copy_from_user((void*) &arg_kernel_space, (void*) arg, sizeof(AMIFL_alloc_params)); - if (arg_kernel_space.size > 128*1024) + if (arg_kernel_space.size > 128 * 1024) { return -EINVAL; } - kmalloc_len = ((arg_kernel_space.size + PAGE_SIZE -1) & PAGE_MASK); - kmalloc_ptr = kmalloc((kmalloc_len + 2 * PAGE_SIZE), GFP_DMA | GFP_KERNEL); - kmalloc_area=(int *)(((unsigned long)kmalloc_ptr + PAGE_SIZE -1) & PAGE_MASK); + kmalloc_len = ((arg_kernel_space.size + PAGE_SIZE - 1) & PAGE_MASK); + kmalloc_ptr = kmalloc((kmalloc_len + 2 * PAGE_SIZE), GFP_DMA | GFP_KERNEL); + kmalloc_area = (int *)(((unsigned long)kmalloc_ptr + PAGE_SIZE - 1) & PAGE_MASK); - for (virt_addr=(unsigned long)kmalloc_area; virt_addr<(unsigned long)kmalloc_area+kmalloc_len; virt_addr+=PAGE_SIZE) + for (virt_addr = (unsigned long) kmalloc_area; + virt_addr < (unsigned long) kmalloc_area + kmalloc_len; + virt_addr += PAGE_SIZE) { mem_map_reserve(virt_to_page(virt_addr)); } - for (i=0; i<(kmalloc_len/sizeof(int)); i++) { - kmalloc_area[i]=(0xafd0<<16) +i; + + { + int i; + for (i = 0; i < (kmalloc_len / sizeof(int)); ++i) { + kmalloc_area[i] = 0xAFD00000 + i; + } } - kmalloc_drv[kcount].size = arg_kernel_space.size; + + kmalloc_drv[kcount].size = arg_kernel_space.size; kmalloc_drv[kcount].kmallocptr = kmalloc_ptr; - kmalloc_drv[kcount].kvirtlen = kmalloc_len; - kmalloc_drv[kcount].kvirtadd = kmalloc_area; - kmalloc_drv[kcount].kphysadd = (void *)((unsigned long)virt_to_phys(kmalloc_area)); - kcount++; + kmalloc_drv[kcount].kvirtlen = kmalloc_len; + kmalloc_drv[kcount].kvirtadd = kmalloc_area; + kmalloc_drv[kcount].kphysadd = (void *)((unsigned long)virt_to_phys(kmalloc_area)); + ++kcount; + arg_kernel_space.kvirtadd = kmalloc_area; arg_kernel_space.kphysadd = (void *)((unsigned long)virt_to_phys(kmalloc_area)); - copy_to_user((void*) arg, (void*) &arg_kernel_space, sizeof(AMIFLDRV_ALLOC)); + copy_to_user((void*) arg, (void*) &arg_kernel_space, sizeof(AMIFL_alloc_params)); return 0; } case CMD_FREE: { - unsigned long virt_addr; - AMIFLDRV_ALLOC arg_kernel_space; + unsigned long virt_addr; + AMIFL_alloc_params arg_kernel_space; int isearch = 0; - copy_from_user((void*) &arg_kernel_space, (void*) arg, sizeof(AMIFLDRV_ALLOC)); + copy_from_user((void*) &arg_kernel_space, (void*) arg, sizeof(AMIFL_alloc_params)); if (kcount > 0) { for (isearch=0; isearchvm_pgoff << PAGE_SHIFT; - unsigned long size = vma->vm_end - vma->vm_start; + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; + unsigned long size = vma->vm_end - vma->vm_start; if (offset & ~PAGE_MASK) { return -ENXIO; @@ -268,7 +305,7 @@ amifldrv_init_module(void) return -EIO; } - memset(kmalloc_drv, 0, sizeof(AMIFLDRV_ALLOC) * 128); + memset(kmalloc_drv, 0, sizeof(AMIFL_alloc_params) * 128); return(0); } @@ -277,26 +314,28 @@ static void /* module_exit */ amifldrv_cleanup_module(void) { unsigned long virt_addr; - int iloop = 0; if (kcount > 0) { - for (iloop=0; iloop < kcount; iloop++) { - kmalloc_ptr = kmalloc_drv[iloop].kmallocptr; - kmalloc_area = kmalloc_drv[iloop].kvirtadd; - kmalloc_len = kmalloc_drv[iloop].kvirtlen; - if (kmalloc_ptr) + int iloop; + for (iloop = 0; iloop < kcount; ++iloop) { - for(virt_addr = (unsigned long)kmalloc_area; - virt_addr < (unsigned long)kmalloc_area + kmalloc_len; - virt_addr += PAGE_SIZE) - { - mem_map_unreserve(virt_to_page(virt_addr)); - } - + kmalloc_ptr = kmalloc_drv[iloop].kmallocptr; + kmalloc_area = kmalloc_drv[iloop].kvirtadd; + kmalloc_len = kmalloc_drv[iloop].kvirtlen; if (kmalloc_ptr) { - kfree(kmalloc_ptr); + for(virt_addr = (unsigned long)kmalloc_area; + virt_addr < (unsigned long)kmalloc_area + kmalloc_len; + virt_addr += PAGE_SIZE) + { + mem_map_unreserve(virt_to_page(virt_addr)); + } + + if (kmalloc_ptr) + { + kfree(kmalloc_ptr); + } } } } diff --git a/amifldrv.h b/amifldrv.h deleted file mode 100644 index bb4eaba..0000000 --- a/amifldrv.h +++ /dev/null @@ -1,13 +0,0 @@ -extern struct file_operations amifldrv_fops; -#define CMD_ALLOC 0x4160 -#define CMD_FREE 0x4161 -#define CMD_LOCK_KB 0x4162 -#define CMD_UNLOCK_KB 0x4163 -typedef struct tagAMIFLDRV_ALLOC -{ - long size; - unsigned long kvirtlen; - void * kmallocptr; - void * kvirtadd; - void * kphysadd; -} AMIFLDRV_ALLOC; diff --git a/amiwrap.c b/amiwrap.c deleted file mode 100644 index 533fd0b..0000000 --- a/amiwrap.c +++ /dev/null @@ -1,191 +0,0 @@ -//------------------------------------------------------------------------------------------------- -// AMI Firmware Update Utility(APTIO) v5.05.04 -// Copyright (C)2011 American Megatrends Inc. All Rights Reserved. -//------------------------------------------------------------------------------------------------- -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) -#include -#endif -#include "amiwrap.h" - -extern int amifldrv_ioctl(void); -extern int amifldrv_mmap(void); -unsigned long ulArg0; -unsigned long ulArg1; -unsigned long ulArg2; -pgprot_t pgArg0; -void *pvArg0; -void *pvArg1; - -AFU_ATTRIBUTE_FUNC static int wrap_open(struct inode *inode, struct file *file); -AFU_ATTRIBUTE_FUNC static int wrap_release(struct inode *inode, struct file *file); -#if defined(HAVE_UNLOCKED_IOCTL) -AFU_ATTRIBUTE_FUNC static long wrap_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); -#else -AFU_ATTRIBUTE_FUNC static int wrap_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg); -#endif -AFU_ATTRIBUTE_FUNC static int wrap_mmap(struct file *file, struct vm_area_struct *vma); - -struct file_operations amifldrv_fops = -{ -owner: - THIS_MODULE, -open: - wrap_open, -release: - wrap_release, -#if defined(HAVE_UNLOCKED_IOCTL) -unlocked_ioctl: - wrap_unlocked_ioctl, -#else -ioctl: - wrap_ioctl, -#endif -mmap: - wrap_mmap, -}; - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) -#define mem_map_reserve(p) set_bit(PG_reserved, &((p)->flags)) -#define mem_map_unreserve(p) clear_bit(PG_reserved, &((p)->flags)) -MODULE_AUTHOR("American Megatrends Inc."); -MODULE_DESCRIPTION("AMI Flash Update utility driver"); -MODULE_LICENSE("Proprietary"); -#endif - -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) -static int device_open_count = 0; -#endif - -AFU_ATTRIBUTE_FUNC -int wrap_open(struct inode *inode, struct file *file) -{ -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - MOD_INC_USE_COUNT; -#else - if (device_open_count) - return -EBUSY; - device_open_count++; - try_module_get(THIS_MODULE); -#endif - return(0); -} - -AFU_ATTRIBUTE_FUNC -int wrap_release(struct inode *inode, struct file *file) -{ -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - MOD_DEC_USE_COUNT; -#else - device_open_count--; - module_put(THIS_MODULE); -#endif - return(0); -} - -AFU_ATTRIBUTE_FUNC -#if defined(HAVE_UNLOCKED_IOCTL) -long wrap_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) -#else -int wrap_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) -#endif -{ -#ifndef HAVE_UNLOCKED_IOCTL - pvArg0 = inode; -#endif - pvArg1 = filp; - ulArg0 = cmd; - ulArg1 = arg; -#if defined(HAVE_UNLOCKED_IOCTL) - return (long)amifldrv_ioctl(); -#else - return amifldrv_ioctl(); -#endif -} - -AFU_ATTRIBUTE_FUNC -int wrap_mmap(struct file *file, struct vm_area_struct *vma) -{ - pvArg0 = file; - pvArg1 = vma; - return amifldrv_mmap(); -} - -AFU_ATTRIBUTE_FUNC -int wrap_register_chrdev() -{ - return register_chrdev((unsigned int)ulArg0, "amifldrv", (struct file_operations *)pvArg0); -} - -AFU_ATTRIBUTE_FUNC -void wrap_unregister_chrdev() -{ - unregister_chrdev((unsigned int)ulArg0, "amifldrv"); -} - -AFU_ATTRIBUTE_FUNC -void wrap_mem_map_reserve() -{ - mem_map_reserve((struct page*)pvArg0); -} - -AFU_ATTRIBUTE_FUNC -void wrap_mem_map_unreserve() -{ - mem_map_unreserve((struct page*)pvArg0); -} - -AFU_ATTRIBUTE_FUNC -struct page *wrap_virt_to_page() -{ - return virt_to_page(ulArg0); -} - -AFU_ATTRIBUTE_FUNC -int wrap_remap_page_range() -{ -#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) - ulArg1 = ulArg1>>PAGE_SHIFT; - return remap_pfn_range((struct vm_area_struct *)pvArg0, ulArg0, ulArg1, ulArg2, pgArg0); -#else - return remap_page_range((struct vm_area_struct *)pvArg0, ulArg0, ulArg1, ulArg2, pgArg0); -#endif -} - -AFU_ATTRIBUTE_FUNC -void *wrap_kmalloc() -{ - return kmalloc((size_t)ulArg0, (int)ulArg1); -} - -AFU_ATTRIBUTE_FUNC -void wrap_kfree() -{ - kfree(pvArg0); -} - -AFU_ATTRIBUTE_FUNC -unsigned long wrap_copy_from_user() -{ - return copy_from_user(pvArg0, pvArg1, ulArg0); -} - -AFU_ATTRIBUTE_FUNC -unsigned long wrap_copy_to_user() -{ - return copy_to_user(pvArg0, pvArg1, ulArg0); -} - diff --git a/amiwrap.h b/amiwrap.h deleted file mode 100644 index b47039a..0000000 --- a/amiwrap.h +++ /dev/null @@ -1,21 +0,0 @@ -//------------------------------------------------------------------------------------------------- -// AMI Firmware Update Utility(APTIO) v5.05.04 -// Copyright (C)2011 American Megatrends Inc. All Rights Reserved. -//------------------------------------------------------------------------------------------------- -#define AFU_ATTRIBUTE_FUNC -AFU_ATTRIBUTE_FUNC int wrap_register_chrdev(void); -AFU_ATTRIBUTE_FUNC void wrap_unregister_chrdev(void); -AFU_ATTRIBUTE_FUNC void wrap_mem_map_reserve(void); -AFU_ATTRIBUTE_FUNC void wrap_mem_map_unreserve(void); -AFU_ATTRIBUTE_FUNC int wrap_remap_page_range(void); -AFU_ATTRIBUTE_FUNC struct page *wrap_virt_to_page(void); -AFU_ATTRIBUTE_FUNC void *wrap_kmalloc(void); -AFU_ATTRIBUTE_FUNC void wrap_kfree(void); -AFU_ATTRIBUTE_FUNC unsigned long wrap_copy_from_user(void); -AFU_ATTRIBUTE_FUNC unsigned long wrap_copy_to_user(void); -extern unsigned long ulArg0; -extern unsigned long ulArg1; -extern unsigned long ulArg2; -extern pgprot_t pgArg0; -extern void *pvArg0; -extern void *pvArg1; diff --git a/linux_iface.h b/linux_iface.h deleted file mode 100644 index 39c4db5..0000000 --- a/linux_iface.h +++ /dev/null @@ -1,11 +0,0 @@ -#include - -#define LINUX_PRE_2_6 (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) -#define LINUX_POST_2_6 (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) - -#if LINUX_PRE_2_6 -# include -#else -# define mem_map_reserve(p) set_bit(PG_reserved, &((p)->flags)) -# define mem_map_unreserve(p) clear_bit(PG_reserved, &((p)->flags)) -#endif