static int set_hw_reason(int reason)
{
	char filename[32] = "";
	int hw_reason = reason;
	struct file *filp = NULL;
	ssize_t nread;

	int pnum = get_partition_num_by_name("misc");

	if (pnum < 0) {
		pr_info("unknown partition number for misc partition\n");
		return 0;
	}

	snprintf(filename, 32, "/dev/block/mmcblk0p%d", pnum);

	filp = filp_open(filename, O_RDWR, 0);
	if (IS_ERR(filp)) {
		pr_info("unable to open file: %s\n", filename);
		return PTR_ERR(filp);
	}

	filp->f_pos = 624;
	nread = kernel_write(filp, (char *)&hw_reason, sizeof(int), filp->f_pos);
	pr_info("wrire: %X (%d)\n", hw_reason, nread);

	if (filp)
		filp_close(filp, NULL);

	return 1;
}
int32 write_conf_to_file(int8* name, int8 * pc_arr)
{
    INI_FILE * fp;
    int32 len;

#ifdef INI_KO_MODULE
    fp = filp_open(NVRAM_CUST_FILE, O_CREAT|O_RDWR, 0664);
	if (IS_ERR(fp))
    {
        INI_DEBUG("open %s failed", NVRAM_CUST_FILE);
        return INI_FAILED;
    }
#else
	fp = fopen(NVRAM_CUST_FILE, "w+");
	if (NULL == fp)
    {
        INI_DEBUG("open %s failed!:%s ", NVRAM_CUST_FILE, strerror(errno));
        return INI_FAILED;
    }
#endif

    INI_DEBUG("open %s succ", NVRAM_CUST_FILE);

#ifdef INI_KO_MODULE
    len = kernel_write(fp, pc_arr, HISI_CUST_NVRAM_LEN, 0);
#else
	len = fwrite(pc_arr, 1, HISI_CUST_NVRAM_LEN, fp);
#endif
    INI_DEBUG("write len:%d", len);

    ini_file_close(fp);

    return INI_SUCC;
}
示例#3
0
文件: big_key.c 项目: 020gzh/linux
/*
 * Preparse a big key
 */
int big_key_preparse(struct key_preparsed_payload *prep)
{
	struct path *path = (struct path *)&prep->payload.data[big_key_path];
	struct file *file;
	ssize_t written;
	size_t datalen = prep->datalen;
	int ret;

	ret = -EINVAL;
	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
		goto error;

	/* Set an arbitrary quota */
	prep->quotalen = 16;

	prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;

	if (datalen > BIG_KEY_FILE_THRESHOLD) {
		/* Create a shmem file to store the data in.  This will permit the data
		 * to be swapped out if needed.
		 *
		 * TODO: Encrypt the stored data with a temporary key.
		 */
		file = shmem_kernel_file_setup("", datalen, 0);
		if (IS_ERR(file)) {
			ret = PTR_ERR(file);
			goto error;
		}

		written = kernel_write(file, prep->data, prep->datalen, 0);
		if (written != datalen) {
			ret = written;
			if (written >= 0)
				ret = -ENOMEM;
			goto err_fput;
		}

		/* Pin the mount and dentry to the key so that we can open it again
		 * later
		 */
		*path = file->f_path;
		path_get(path);
		fput(file);
	} else {
		/* Just store the data in a buffer */
		void *data = kmalloc(datalen, GFP_KERNEL);
		if (!data)
			return -ENOMEM;

		prep->payload.data[big_key_data] = data;
		memcpy(data, prep->data, prep->datalen);
	}
	return 0;

err_fput:
	fput(file);
error:
	return ret;
}
static ssize_t htc_debug_write(struct file *file, const char __user *buffer,
                        size_t count, loff_t *ppos)
{
    char buf[FLAG_LEN+3];
    char filename[32] = "";
    struct file *filp = NULL;
    ssize_t nread;
    int pnum;

    SECMSG("%s called (count:%d)\n", __func__, (int)count);

    if (count != sizeof(buf)){
        printk(KERN_ERR"count != sizeof(buf)\n");
        return -EFAULT;
    }

    if (copy_from_user(buf, buffer, count))
        return -EFAULT;

    memset(htc_debug_flag,0,FLAG_LEN+1);
    memcpy(htc_debug_flag,buf+2,FLAG_LEN);

    SECMSG("Receive :%s\n",buf);
    SECMSG("Flag    :%s\n",htc_debug_flag);

    pnum = get_partition_num_by_name("misc");

    if (pnum < 0) {
        printk(KERN_ERR"unknown partition number for misc partition\n");
        return 0;
    }

    snprintf(filename, 32, "/dev/block/mmcblk0p%d", pnum);

    filp = filp_open(filename, O_RDWR, 0);
    if (IS_ERR(filp)) {
        printk(KERN_ERR"unable to open file: %s\n", filename);
        return PTR_ERR(filp);
    }

    SECMSG("%s: offset :%d\n", __func__, offset);
    filp->f_pos = offset;
    nread = kernel_write(filp, buf, FLAG_LEN+2, filp->f_pos);
    SECMSG("%s:wrire buf: %s (%zd)\n", __func__, buf, nread);

    if (filp)
        filp_close(filp, NULL);

    return count;
}
// /data/aaa
int my_init(void) {
	char *buff;
	struct file *filp;
	int ret;
	filp = filp_open( "/data/bbb", O_WRONLY|O_TRUNC|O_CREAT, 0666 );
	if( filp == 0 )
		return 0;
	buff = (char*)__get_free_pages( GFP_KERNEL, 0 );
	strcpy(buff, "hello world");
	ret = kernel_write( filp, buff, strlen(buff), 0 );
	printk("ret=%d\n", ret );
	free_pages( (unsigned long)buff, 0 );
	filp_close( filp, 0 );
	return 0;
}
示例#6
0
static int write_env_area(char *env_buf)
{
	int i,checksum = 0;
	int result = 0;
	int ret = 0;
	loff_t pos = 0;
	mm_segment_t old_fs;
	struct file *write_fp;
	
	memcpy(env_buf, ENV_SIG, sizeof(g_env.sig_head));
	memcpy(env_buf + CFG_ENV_SIG_1_OFFSET, ENV_SIG, sizeof(g_env.sig_tail));

	for (i = 0; i < (CFG_ENV_DATA_SIZE); i++) {
		checksum += *(env_buf + CFG_ENV_DATA_OFFSET + i);
	}
	*((int *)env_buf + CFG_ENV_CHECKSUM_OFFSET / 4) = checksum;

	write_fp = filp_open(MISC_PATH, O_RDWR, 0);
	if (IS_ERR(write_fp)) {
			result = PTR_ERR(write_fp);
			pr_err("[%s]File open return fail,result=%d file=%p\n", 
				MODULE_NAME, result, write_fp);
			goto filp_open_fail;
	}

	pos += CFG_ENV_OFFSET;
	ret = kernel_write(write_fp, (u32*)env_buf, CFG_ENV_SIZE, pos);
	if (ret < 0) {
		pr_err("[%s]Kernel write env fail\n", MODULE_NAME);
		result = -1;
	}

	old_fs = get_fs();
	set_fs(get_ds());
	ret = vfs_fsync(write_fp, 0);
	if (ret < 0) {
	    pr_warn("[%s]Kernel write env sync fail\n", MODULE_NAME);
	}
	set_fs(old_fs);

	filp_close(write_fp, 0);
filp_open_fail:
	return (result);
}
示例#7
0
文件: init.c 项目: gdarcy/scaraOS
/* Init task - the job of this task is to initialise all
 * installed drivers, mount the root filesystem and
 * bootstrap the system */
int init_task(void *priv)
{
	uint32_t ret;
	struct file *file;
	char buf[20];
	int rval;

	/* Initialise kernel subsystems */
	blk_init();
	vfs_init();
	pci_init();

	do_initcalls();

	/* Mount the root filesystem etc.. */
	if ( vfs_mount_root("ext2", "floppy0") ) {
		panic("Unable to mount root filesystem\n");
	}

	file = kernel_open("/test.txt", 0);
	if ( NULL == file ) {
		printk("init_task: open failed, returned %u\n", -1);
	} else {
		rval = kernel_read(file, buf, 16);
		if ( rval < 0 )
			printk("read error: %d\n", rval);
		else if ( rval == 0 )
			printk("read returned EOF\n");
		else {
			buf[rval] = '\0';
			printk("read: %s.\n", buf);
		}
		rval = kernel_write(file, buf, 16);
		kernel_close(file);
	}

	ret = _kernel_exec("/bin/bash");
	ret = _kernel_exec("/sbin/init");
	ret = _kernel_exec("/bin/cat");
	printk("exec: /sbin/init: %i\n", (int)ret);

	return ret;
}
示例#8
0
文件: write.c 项目: gdarcy/scaraOS
int _sys_write(unsigned int handle, const char *buf, size_t count)
{
	struct file *fd;
	struct task *me;
	char *kbuf;

	if ( handle == 1 ) {
		return _sys_write_stdout(handle, (void *)buf, count);
	}

	kbuf = strdup_from_user(buf, UACCESS_KERNEL_OK);
	if ( NULL == kbuf )
		return -1; /* EFAULT or ENOMEM */

	me = __this_task;
	fd = fdt_entry_retr(me->fd_table, handle);
	if ( NULL == fd )
		return -1; /* EMAXFILE */
	return kernel_write(fd, kbuf, count);
}
示例#9
0
static unsigned long get_spendcnt_from_datefile()
{
	struct file* fp;
	int buf_len;
	char _tmp_buf[20];
	char value[20];
	char *p = value;
	unsigned long spendcnt_poweroff = 0;

	//buf_len = strlen(buf);
	fp = filp_open(DATE_FILENAME,O_RDONLY,0);
	if(IS_ERR(fp))
    {
		printk("bryan---->open file /data/time_record.dat failed\n");
		return spendcnt_poweroff;
	}
	else
	{
		sprintf(_tmp_buf, "%lu", batteryspendcnt);
		buf_len = strlen(_tmp_buf);
		kernel_write(fp, _tmp_buf, buf_len, 0);

		kernel_read(fp,0,value,10);
	    filp_close(fp,NULL);

		value[20]=0;
		while(*p)
		{
		    if(*p==0x0d)
		    {
				*p=0;
				break;
			}
			p++;
		}	
		sscanf(value,"%d",&spendcnt_poweroff);
		printk("bryan---->last poweroff_spendcnt = %d\n",spendcnt_poweroff);
	}
	return spendcnt_poweroff;
}
示例#10
0
static void save_spendcnt_to_datefile()
{
	struct file* fp;
	int buf_len;
	char _tmp_buf[20];
	char value[20];
	char *p = value;
	unsigned long spendcnt_poweroff = 0;

	fp = filp_open(DATE_FILENAME,O_WRONLY | O_CREAT,0);
	if(IS_ERR(fp))
    {
		printk("bryan---->open file /data/time_record.dat failed\n");
	}
	else
	{
		sprintf(_tmp_buf, "%lu", batteryspendcnt);
		buf_len = strlen(_tmp_buf);
		kernel_write(fp, _tmp_buf, buf_len, 0);		
	    filp_close(fp,NULL);
	}
}
示例#11
0
/**
 * Synchronize memory mapping with the file.
 * Called from process context.
 */
static void
tempesta_unmap_file(struct file *file, unsigned long addr, unsigned long len,
		    int node)
{
	mm_segment_t oldfs;
	MArea *ma;
	loff_t off = 0;
	ssize_t r;

	mutex_lock(&map_mtx);

	ma = ma_lookup(addr, node);
	if (!ma) {
		TDB_WARN("Cannot sync memory area for %#lx address at node"
			 " %d\n", addr, node);
		goto err;
	}

	oldfs = get_fs();
	set_fs(get_ds());

	r = kernel_write(file, (void *)ma->start, len, &off);
	if (r != len) {
		TDB_WARN("Cannot sync mapping %lx of size %lu pages\n",
			 ma->start, ma->pages);
		goto err_fs;
	}

err_fs:
	set_fs(oldfs);
err:
	fput(file);
	ma_free(addr, node);

	mutex_unlock(&map_mtx);
}
ssize_t expdb_write(struct file *filp, const char *buf, size_t len, loff_t *off)
{
	return kernel_write(filp, buf, len, off);
}
示例#13
0
static void rk2918_battery_timer_work(struct work_struct *work)
{		
	rk2918_get_bat_status(gBatteryData);
	rk2918_get_bat_health(gBatteryData);
	rk2918_get_bat_present(gBatteryData);
	rk2918_get_bat_voltage(gBatteryData);
	//to prevent gBatCapacity be changed sharply
	if (gBatCapacity < 0)
	{
		gBatCapacity = 0;
	}
	else
	{
		if (gBatCapacity > 100)
		{
			gBatCapacity = 100;
		}
	}
	rk2918_get_bat_capacity(gBatteryData);
	
	if (rk29_battery_dbg_level)
	{
    	if (++AdcTestCnt >= 20)
    	{
    	    AdcTestCnt = 0;
    	    printk("\nchg_ok_level =%d, chg_ok= %d, gBatStatus = %d, adc_val = %d, TrueBatVol = %d,gBatVol = %d, gBatCap = %d, captmp = %d, sec = %lu, time_chg_flag = %d, first_flag  = %d\n", 
    	            gBatteryData->charge_ok_level, gpio_get_value(gBatteryData->charge_ok_pin), gBatStatus, AdcTestvalue, adc_to_voltage(AdcTestvalue), 
    	            gBatVoltage, gBatCapacity, capacitytmp, batteryspendcnt, time_chg_flag, first_flag);
    	}
    }

	/*update battery parameter after adc and capacity has been changed*/
	if(((gBatStatus != gBatLastStatus) || (gBatPresent != gBatLastPresent) || (gBatCapacity != gBatLastCapacity))&&(suspend_flag==0))
	{
		//for debug
		if (rk29_battery_dbg_level)
		{
			char _tmp_buf[250];
			int buf_len = 0;
			struct file* fp;
			sprintf(_tmp_buf, "gBatStatus = %d, adc_val = %d, TrueBatVol = %d,gBatVol = %d, gBatCap = %d, captmp = %d, sec = %lu, inter_sec = %lu, time_chg_flag = %d, first_flag = %d\n", 
		    	            gBatStatus, AdcTestvalue, ((AdcTestvalue * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R)), 
		    	            gBatVoltage, gBatCapacity, capacitytmp, batteryspendcnt, batteryspendcnt - last_batteryspendcnt, time_chg_flag, first_flag);
			buf_len = strlen(_tmp_buf);
			fp = filp_open(BATT_DBG_FILE,O_RDWR | O_APPEND | O_CREAT, 0);
			if(IS_ERR(fp))
			{
				printk("bryan---->open file /data/bat_dbg_record.dat failed\n");
			}
			else
			{
				kernel_write(fp, _tmp_buf, buf_len ,buf_offset);
			    filp_close(fp,NULL);
				buf_offset += buf_len; 
			}
			last_batteryspendcnt = batteryspendcnt;
		}
		
		gBatLastStatus = gBatStatus;
		gBatLastPresent = gBatPresent;
		gBatLastCapacity = gBatCapacity;
		power_supply_changed(&gBatteryData->battery);
	}
}
示例#14
0
文件: big_key.c 项目: mkrufky/linux
/*
 * Preparse a big key
 */
int big_key_preparse(struct key_preparsed_payload *prep)
{
	struct path *path = (struct path *)&prep->payload.data[big_key_path];
	struct file *file;
	u8 *enckey;
	u8 *data = NULL;
	ssize_t written;
	size_t datalen = prep->datalen;
	int ret;

	ret = -EINVAL;
	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
		goto error;

	/* Set an arbitrary quota */
	prep->quotalen = 16;

	prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;

	if (datalen > BIG_KEY_FILE_THRESHOLD) {
		/* Create a shmem file to store the data in.  This will permit the data
		 * to be swapped out if needed.
		 *
		 * File content is stored encrypted with randomly generated key.
		 */
		size_t enclen = ALIGN(datalen, crypto_skcipher_blocksize(big_key_skcipher));
		loff_t pos = 0;

		/* prepare aligned data to encrypt */
		data = kmalloc(enclen, GFP_KERNEL);
		if (!data)
			return -ENOMEM;

		memcpy(data, prep->data, datalen);
		memset(data + datalen, 0x00, enclen - datalen);

		/* generate random key */
		enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
		if (!enckey) {
			ret = -ENOMEM;
			goto error;
		}

		ret = big_key_gen_enckey(enckey);
		if (ret)
			goto err_enckey;

		/* encrypt aligned data */
		ret = big_key_crypt(BIG_KEY_ENC, data, enclen, enckey);
		if (ret)
			goto err_enckey;

		/* save aligned data to file */
		file = shmem_kernel_file_setup("", enclen, 0);
		if (IS_ERR(file)) {
			ret = PTR_ERR(file);
			goto err_enckey;
		}

		written = kernel_write(file, data, enclen, &pos);
		if (written != enclen) {
			ret = written;
			if (written >= 0)
				ret = -ENOMEM;
			goto err_fput;
		}

		/* Pin the mount and dentry to the key so that we can open it again
		 * later
		 */
		prep->payload.data[big_key_data] = enckey;
		*path = file->f_path;
		path_get(path);
		fput(file);
		kfree(data);
	} else {
		/* Just store the data in a buffer */
		void *data = kmalloc(datalen, GFP_KERNEL);

		if (!data)
			return -ENOMEM;

		prep->payload.data[big_key_data] = data;
		memcpy(data, prep->data, prep->datalen);
	}
	return 0;

err_fput:
	fput(file);
err_enckey:
	kfree(enckey);
error:
	kfree(data);
	return ret;
}