asmlinkage long sys_csci3411_set_attr ( char *filename, char *attrname, char *attrvalue, int size ) { struct stat sb ; /* necessary structure for using stat() */ char *fstring ; /* file/directory to give attribute to */ char *dstring ; /* temporary string for strstr() */ char buf[128] ; /* copy of the original filename */ char loc[128] ; /* full filepath for attr folder */ char cmd[128] ; /* command for system calls */ bool_t filetype ; /* is the tag for a file or dir? */ int fd ; /* file descriptor for attribute file */ mm_segment_t fs ; /* FS segment used to make sys calls */ fs = get_fs(); set_fs(get_ds()); /* check if filename is an existing file or directory */ if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) ) filetype = CSCI3411_FILE; else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) ) filetype = CSCI3411_DIR; else return -1; /* file/directory does not exist */ /* split filename strings into containing folder and file */ fstring = strrchr(filename, '/'); fstring += sizeof(char); copy_from_user( buf, filename, sizeof(buf)/sizeof(char)); dstring = strstr( buf, fstring ); strncpy( dstring,"\0",1); sprintf(loc,"%s.%s_attr",buf,fstring); /* check if attributes directory exists, mkdir if it doesn't */ if( sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode) ); else sys_mkdir(loc,sb.st_mode); /* create file in attribute directory * in the event of a duplicate attribute file, * overwrite with newer information */ sprintf(cmd,"%s/%s",loc,attrname); sys_unlink(cmd); //remove file if it exists so we can make a new one sys_mknod(cmd,sb.st_mode,0); fd = sys_open(cmd,O_CREAT|O_WRONLY,0); if(fd<0) printk("file creation failed\n"); else sys_write(fd,attrvalue,size); set_fs(fs); return 0; }
asmlinkage long sys_csci3411_get_attr(char *filename, char *attrname, char *buf, int bufsize){ struct stat sb ; /* necessary structure for using sys_newstat() */ char *fstring ; /* file/directory to get attribute from */ char *dstring ; /* temporary string for strstr() */ char loc[128] ; /* full filepath for attr folder */ char buff[128] ; /* temporary buffer for filename */ char cmd[128] ; /* buffer holds full path to attrname */ char attrvalue[128];/* Temporary buffer for attribute value */ int attr_fd ; /* The file we are reading from */ bool_t filetype ; /* is the tag for a file or dir? */ mm_segment_t fs ; /* FS segment used to make sys calls */ fs = get_fs(); set_fs(get_ds()); /* check if filename is an existing file or directory */ if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) ) filetype = CSCI3411_FILE; else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) ) filetype = CSCI3411_DIR; else return -1; /* file/directory does not exist */ /* split filename strings into containing folder and file */ fstring = strrchr(filename, '/'); fstring += sizeof(char); copy_from_user( buff, filename, sizeof(buff)/sizeof(char)); dstring = strstr( buff, fstring ); strncpy( dstring,"\0",1); sprintf(loc,"%s.%s_attr",buff,fstring); // Check if the directory is valid if(!(sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode))) return -1; // Open the file and read its attrvalue if it exists sprintf(cmd,"%s/%s",loc,attrname); if(!(sys_newstat(cmd,&sb)==0 && S_ISREG(sb.st_mode))) return -1; attr_fd = sys_open(cmd, O_RDWR, 0); sys_read(attr_fd, attrvalue, sb.st_size); sys_close(attr_fd); // Copy the attrvalue to buf copy_to_user(buf, attrvalue, sb.st_size); set_fs(fs); // Return the size of the attribute value return(sb.st_size); }
asmlinkage long sys_csci3411_remove_attr ( char *filename, char *attrname ) { struct stat sb ; /* necessary structure for using sys_newstat() */ char *fstring ; /* file/directory to remove attribute for */ char *dstring ; /* temporary string for strstr() */ char buf[128] ; /* copy of the original filename */ char loc[128] ; /* full filepath for attr folder */ char loc2[128] ; /* full filepath for attrname file */ bool_t filetype ; /* tag for file/directory */ mm_segment_t fs ; /* FS segment used to make sys calls */ fs = get_fs(); set_fs(get_ds()); /* check if filename is an existing file or directory */ if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) ) filetype = CSCI3411_FILE; else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) ) filetype = CSCI3411_DIR; else return -1; /* file/directory does not exist */ /* split filename strings into containing folder and file */ fstring = strrchr(filename, '/'); fstring += sizeof(char); copy_from_user( buf, filename, sizeof(buf)/sizeof(char)); dstring = strstr( buf, fstring ); strncpy( dstring,"\0",1); sprintf(loc,"%s.%s_attr",buf,fstring); /* check if attributes directory exists, return error and exit if not */ if( sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode) ) { sprintf(loc2,"%s/%s",loc,attrname); if( sys_newstat(loc2,&sb)==0 && S_ISREG(sb.st_mode) ) { if( sys_unlink(loc2) ) return -1; sys_rmdir(loc); } else return -1; /* file/directory does not exist */ } else return -1; /* file/directory does not exist */ set_fs(fs); return 0; }
static int getcheck(char* filename) { struct stat sstat; if (sys_newstat(filename, &sstat) < 0) { lab_printf("fs: getcheck: Sorry, %s seems to be missing - or at least, not stat'able.\n", filename); return 0; } return 1; }
static unsigned char* get(int* count, char* filename) { unsigned char* ptr, *ptr2; struct stat sstat; int fd; int remaining; int sz, total=0; set_fs(KERNEL_DS); if (sys_newstat(filename, &sstat) < 0) return 0; *count = remaining = sstat.st_size; ptr = ptr2 = vmalloc(sstat.st_size+32); if (!ptr) { printk("lab: AIIIIIIIIIIIIIIIIIGHHH!! out of memory!!\n"); printk("lab: I guess we just can't allocate 0x%08x bytes???\n", (unsigned int)sstat.st_size+32); return 0; } if ((fd=sys_open(filename,O_RDONLY,0)) < 0) { printk("lab: uh. couldn't open the file.\n"); vfree(ptr); return 0; } while (remaining) { sz = (1048576 < remaining) ? 1048576 : remaining; total += sz; if (sys_read(fd,ptr2,sz) < sz) { printk("lab: uh. couldn't read from the file.\n"); sys_close(fd); vfree(ptr); return 0; } ptr2 += sz; remaining -= sz; } sys_close(fd); return ptr; }
int FirmwareUpgrade (struct synaptics_ts_data *ts, const char* fw_path) { int ret = 0; int fd = -1; mm_segment_t old_fs = 0; struct stat fw_bin_stat; unsigned long read_bytes; if (unlikely(fw_path[0] != 0)) { old_fs = get_fs(); set_fs(get_ds()); fd = sys_open((const char __user *) fw_path, O_RDONLY, 0); if (fd < 0) { TOUCH_ERR_MSG("Can not read FW binary from %s\n", fw_path); ret = -EEXIST; goto read_fail; } ret = sys_newstat((char __user *) fw_path, (struct stat *)&fw_bin_stat); if (ret < 0) { TOUCH_ERR_MSG("Can not read FW binary stat from %s\n", fw_path); goto fw_mem_alloc_fail; } my_image_size = fw_bin_stat.st_size; my_image_bin = kzalloc(sizeof(char) * (my_image_size+1), GFP_KERNEL); if (my_image_bin == NULL) { TOUCH_ERR_MSG("Can not allocate memory\n"); ret = -ENOMEM; goto fw_mem_alloc_fail; } read_bytes = sys_read(fd, (char __user *)my_image_bin, my_image_size); /* for checksum */ *(my_image_bin+my_image_size) = 0xFF; TOUCH_INFO_MSG("Touch FW image read %ld bytes from %s\n", read_bytes, fw_path); } else { my_image_size = ts->fw_info.fw_size-1; my_image_bin = (unsigned char *)(&ts->fw_info.fw_start[0]); } CompleteReflash(ts); /* ret = CompleteReflash(ts); if (ret < 0) { TOUCH_ERR_MSG("CompleteReflash_Lockdown fail\n"); } */ if (unlikely(fw_path[0] != 0)) kfree(my_image_bin); fw_mem_alloc_fail: sys_close(fd); read_fail: set_fs(old_fs); return ret; }
asmlinkage long sys_csci3411_remove_attr_all ( char *filename ) { struct stat sb ; /* Necessary structure for using sys_newstat() */ char *fstring ; /* File/directory to give attribute to */ char *dstring ; /* temporary string for strstr() */ char loc[128] ; /* Full filepath for attr folder */ char loc2[128] ; /* holds filepath for attrname file to remove */ char buff[128] ; /* holds copy of original filename */ char buf2[1024]; /* buffer used for the sys_getdents() call */ char total_attr[1024] = "\0"; /* Contains all the attribute names */ struct linux_dirent *dent=NULL; /* Necessary for looking through the directory */ bool_t filetype ; /* tag for file/directory */ int i,nread,dir ; /* necessary values for sys_getdents() */ char *aPtr ; /* holds strsep tokens */ char *nPtr ; /* points to colon-seperated list */ mm_segment_t fs ; /* FS segment used to make sys calls */ fs = get_fs(); set_fs(get_ds()); /* Check if filename is an existing file or directory */ if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) ) filetype = CSCI3411_FILE; else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) ) filetype = CSCI3411_DIR; else return -1; /* file/directory does not exist */ /* Split filename strings into containing folder and file */ fstring = strrchr(filename, '/'); fstring += sizeof(char); copy_from_user( buff, filename, sizeof(buff)/sizeof(char)); dstring = strstr( buff, fstring ); strncpy( dstring,"\0",1); sprintf(loc,"%s.%s_attr",buff,fstring); // Check if the directory is valid if(!(sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode))) return -1; // Go inside the directory dir = sys_open(loc,O_RDONLY,0); if (dir == -1) return -1; for( ; ; ) { nread = sys_getdents(dir,buf2,1024); if(nread==-1) return -1; if(nread==0) break; // Read each file in the directory for(i = 0; i<nread;) { dent = (struct linux_dirent *)(buf2+i); // The first two entries are "." and "..", skip those if(strcmp((char *)(dent->d_name),".") && strcmp((char *)(dent->d_name),"..")) { // Put all the file names in total_attr strcat(total_attr, (char *)(dent->d_name)); strcat(total_attr, ":"); } i+= dent->d_reclen; } } sys_close(dir); total_attr[strlen(total_attr)-1] = '\0'; // remove last ":" printk("%s\n",total_attr); nPtr = total_attr; do { aPtr = strsep(&nPtr,":"); if(aPtr) { printk("%s\n",aPtr); /* remove file */ memset(loc2,'\0',128); // reset the string! sprintf(loc2,"%s/%s",loc,aPtr); if( sys_unlink(loc2) ) return -1; sys_rmdir(loc); } }while(aPtr); set_fs(fs); return 0; }
asmlinkage long sys_csci3411_get_attr_names(char *filename, char *buf, int bufsize){ struct stat sb ; /* Necessary structure for using sys_newstat() */ char *fstring ; /* File/directory to get attribute names from */ char *dstring ; /* temporary string for strstr() */ char loc[128] ; /* Full filepath for attr folder */ char buff[128] ; /* copy of the original filename */ char buf2[1024] ; /* buffer used for the sys_getdents() call */ char total_attr[1024] = "\0" ; /* Contains all the attribute names */ struct linux_dirent *dent=NULL ; /* Necessary for looking through the directory */ int dir,i,nread;; /* necessary for opening and looking into directory */ bool_t filetype ; /* is the tag for a file or dir? */ mm_segment_t fs ; /* FS segment used to make sys calls */ fs = get_fs(); set_fs(get_ds()); /* Check if filename is an existing file or directory */ if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) ) filetype = CSCI3411_FILE; else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) ) filetype = CSCI3411_DIR; else return -1; /* file/directory does not exist */ /* Split filename strings into containing folder and file */ fstring = strrchr(filename, '/'); fstring += sizeof(char); copy_from_user( buff, filename, sizeof(buff)/sizeof(char)); dstring = strstr( buff, fstring ); strncpy( dstring,"\0",1); sprintf(loc,"%s.%s_attr",buff,fstring); // Check if the directory is valid if(!(sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode))) return -1; // Go inside the directory dir = sys_open(loc,O_RDONLY,0); if (dir == -1) return -1; for( ; ; ) { nread = sys_getdents(dir,buf2,1024); if(nread==-1) return -1; if(nread==0) break; // Read each file in the directory for(i = 0; i<nread;) { dent = (struct linux_dirent *)(buf2+i); // The first two entries are "." and "..", skip those if(strcmp((char *)(dent->d_name),".") && strcmp((char *)(dent->d_name),"..")) { // Put all the file names in total_attr strcat(total_attr, (char *)(dent->d_name)); strcat(total_attr, ":"); } i+= dent->d_reclen; } } sys_close(dir); total_attr[strlen(total_attr)-1] = '\0'; // remove last ":" // Copy all the names into buf copy_to_user(buf, total_attr, bufsize); set_fs(fs); // Return the number of bytes copied return(strlen(total_attr)); }