Esempio n. 1
1
struct cpu_state* syscall(struct cpu_state* cpu) {
    save_cpu_state(cpu);

    cpu = get_current_task()->cpuState;

	switch (cpu->eax) {
	case 1: /* exit */
		return terminate_current(cpu);

    case 3: /* exec */
    {
        cpu->eax = vfs_exec((char*) cpu->ebx, (char**) cpu->ecx);
    }
        break;

    case 4: /* getargs */
    {
        cpu->eax = (uint32_t) get_current_task()->args;
    }
        break;

    case 5: /* yield */
    {
        cpu = schedule(cpu);
    }
        break;

	case 10: /* fopen */
	{
	    char* name = strclone((char*) cpu->ebx);
	    uint32_t fmode = (uint32_t) cpu->ecx;

	    struct res_handle* handle = vfs_open(name, fmode);
	    if(handle) {
	        register_handle(handle);
	        cpu->eax = (uint32_t) handle;
	    }
	    else
	    {
	        cpu->eax = 0;
	    }

	    free(name);
	}
	    break;

	case 11: /* fclose */
	{
	    struct res_handle* handle = (void*) cpu->ebx;
	    if(!unregister_handle(handle)) {
	        vfs_close(handle);

	        cpu->eax = 0;
	    }
	    else
	    {
	        cpu->eax = (uint32_t) -1;
	    }
	}
	    break;

	case 12: /* fwrite */
	{
	    struct res_handle* handle = (void*) cpu->ebx;
	    if(handle != 0) {
	        cpu->eax = vfs_write(handle, (char*) cpu->ecx, cpu->edx, 1);
	    }
	    else
	    {
            cpu->eax = RW_ERR_VFS;
	    }
	}
	    break;

	case 13: /* fread */
	{
        struct res_handle* handle = (void*) cpu->ebx;
        if(handle != 0) {
            cpu->eax = vfs_read(handle, (char*) cpu->ecx, cpu->edx, 1);
        }
        else
        {
            cpu->eax = RW_ERR_VFS;
        }
	}
	    break;

	case 14: /* fmkfifo */
	{
        char* name = strclone((char*) cpu->ebx);
        vfs_create_kfile(name, ramfs_fifo_driver_struct(), &(uint32_t){4096}); //default to 4k Buffer-size

        struct res_handle* handle = vfs_open(name, FM_READ | FM_WRITE);
        if(handle) {
            register_handle(handle);
            cpu->eax = (uint32_t) handle;
        }
        else
        {
            cpu->eax = 0;
        }

        free(name);
	}
	    break;

	case 20: /* getpmhandle */
	{
	    struct res_handle* handle = 0;

	    switch(cpu->ebx) {
	    case PMID_STDOUT:
	        handle = get_current_task()->stdout;
	        break;
        case PMID_STDIN:
            handle = get_current_task()->stdin;
            break;
        case PMID_STDERR:
            handle = get_current_task()->stderr;
            break;
        default:
            handle = get_current_task()->stdout;
            break;
	    }

	    cpu->eax = (uint32_t) handle;
	}
	    break;

	case 21: /* fopenpmhandle */
	{
	    char* path = strclone((char*)cpu->ecx);

	    struct res_handle* open;
	    uint32_t fm = FM_WRITE;

	    if(cpu->ebx == PMID_STDIN) {
	        fm = FM_READ;
	    }

	    open = vfs_open(path, fm);

	    free(path);

	    if(!open) {
	        cpu->eax = (uint32_t) -1;
	        break;
	    }

	    struct res_handle* oldhandle = 0;

        switch(cpu->ebx) {
        case PMID_STDOUT:
            oldhandle = get_current_task()->stdout;
            get_current_task()->stdout = open;
            break;
        case PMID_STDIN:
            oldhandle = get_current_task()->stdin;
            get_current_task()->stdin = open;
            break;
        case PMID_STDERR:
            oldhandle = get_current_task()->stderr;
            get_current_task()->stderr = open;
            break;
        default:
            oldhandle = get_current_task()->stdout;
            get_current_task()->stdout = open;
            break;
        }

        if(oldhandle != 0) {
            vfs_close(oldhandle);
        }

        cpu->eax = 0;
	}
	    break;

	case 201: /* kputc */
		cpu->eax = kprintf("%c", cpu->ebx);
		break;

	case 202: /* kputs */
		cpu->eax = kprintf("%s", cpu->ebx);
		break;

	case 203: /* vmm_alloc_ucont */
		cpu->eax = (uint32_t) vmm_alloc_ucont(cpu->ebx);
		break;

	case 204: /* vmm_free */
		cpu->eax = 0;
		if (cpu->ebx >= PROGRAM_BOTTOM) { //Only in PROGRAM AREA ;)
			vmm_free((void*) cpu->ebx);
		}
		break;

	case 205: /* pmm_print_stats */
		pmm_print_stats();
		break;

	default:
		kprintf("Invalid Syscall %d...", cpu->eax);
		break;
	}

	return cpu;
}
void ssp_dump_task(struct work_struct *work) {
	struct ssp_big *big;
	struct file *dump_file;
	struct ssp_msg *msg;
	char *buffer;
	char strFilePath[60];
	struct timeval cur_time;
	int iTimeTemp;
	mm_segment_t fs;
	int buf_len, packet_len, residue, iRet = 0, index = 0 ,iRetTrans=0 ,iRetWrite=0;


	big = container_of(work, struct ssp_big, work);
	pr_err("[SSP]: %s - start ssp dumping (%d)(%d)\n", __func__,big->data->bMcuDumpMode,big->data->uDumpCnt);
	big->data->uDumpCnt++;
	wake_lock(&big->data->ssp_wake_lock);


	fs = get_fs();
	set_fs(get_ds());

	if(big->data->bMcuDumpMode == true)
	{
		do_gettimeofday(&cur_time);
		iTimeTemp = (int) cur_time.tv_sec;

		sprintf(strFilePath, "%s%d.txt", DUMP_FILE_PATH, iTimeTemp);

		dump_file = filp_open(strFilePath, O_RDWR | O_CREAT | O_APPEND, 0666);
		if (IS_ERR(dump_file)) {
			pr_err("[SSP]: %s - Can't open dump file\n", __func__);
			set_fs(fs);
			iRet = PTR_ERR(dump_file);
			wake_unlock(&big->data->ssp_wake_lock);
			return;
		}
	}
	else
		dump_file = NULL;


	buf_len = big->length > DATA_PACKET_SIZE ? DATA_PACKET_SIZE : big->length;
	buffer = kzalloc(buf_len, GFP_KERNEL);
	residue = big->length;

	while (residue > 0) {
		packet_len = residue > DATA_PACKET_SIZE ? DATA_PACKET_SIZE : residue;

		msg = kzalloc(sizeof(*msg), GFP_KERNEL);
		msg->cmd = MSG2SSP_AP_GET_BIG_DATA;
		msg->length = packet_len;
		msg->options = AP2HUB_READ | (index++ << SSP_INDEX);
		msg->data = big->addr;
		msg->buffer = buffer;
		msg->free_buffer = 0;

		iRetTrans = ssp_spi_sync(big->data, msg, 1000);
		if (iRetTrans != SUCCESS) {
			pr_err("[SSP]: %s - Fail to receive data %d (%d)\n", __func__, iRetTrans,residue);
			break;
		}
			if(big->data->bMcuDumpMode == true)
			{
				iRetWrite = vfs_write(dump_file, (char __user *) buffer, packet_len,
					&dump_file->f_pos);
				if (iRetWrite < 0) {
					pr_err("[SSP]: %s - Can't write dump to file\n", __func__);
					break;
				}
		}

		residue -= packet_len;
	}


	if(big->data->bMcuDumpMode == true && (iRetTrans != SUCCESS || iRetWrite < 0) )
	{
		char FAILSTRING[100];
		sprintf(FAILSTRING,"FAIL OCCURED(%d)(%d)(%d)",iRetTrans,iRetWrite,big->length);
		vfs_write(dump_file, (char __user *) FAILSTRING, strlen(FAILSTRING),&dump_file->f_pos);
	}

	ssp_send_cmd(big->data, MSG2SSP_AP_MCU_DUMP_FINISH, SUCCESS);

	big->data->bDumping = false;
	if(big->data->bMcuDumpMode == true)
	{
		filp_close(dump_file, current->files);
	}
	set_fs(fs);
	sanity_check(big->data);

	wake_unlock(&big->data->ssp_wake_lock);
	kfree(buffer);
	kfree(big);

	pr_err("[SSP]: %s done\n", __func__);
}
Esempio n. 3
0
tEplKernel PUBLIC AppCbEvent(
    tEplApiEventType        EventType_p,   // IN: event type (enum)
    tEplApiEventArg*        pEventArg_p,   // IN: event argument (union)
    void GENERIC*           pUserArg_p)
{
    tEplKernel          EplRet = kEplSuccessful;

    UNUSED_PARAMETER(pUserArg_p);

    // check if NMT_GS_OFF is reached
    switch (EventType_p)
    {
    case kEplApiEventNmtStateChange:
    {
        switch (pEventArg_p->m_NmtStateChange.m_NewNmtState)
        {
        case kEplNmtGsOff:
        {   // NMT state machine was shut down,
            // because of user signal (CTRL-C) or critical EPL stack error
            // -> also shut down EplApiProcess() and main()
            EplRet = kEplShutdown;

            PRINTF("%s(kEplNmtGsOff) originating event = 0x%X\n", __func__, pEventArg_p->m_NmtStateChange.m_NmtEvent);

            // wake up EplLinExit()
            atomic_set(&AtomicShutdown_g, TRUE);
            wake_up_interruptible(&WaitQueueShutdown_g);
            break;
        }

        case kEplNmtGsResetCommunication:
        {
            // continue
        }

        case kEplNmtGsResetConfiguration:
        {
            if (uiCycleLen_g != 0)
            {
                EplRet = EplApiWriteLocalObject(0x1006, 0x00, &uiCycleLen_g, sizeof (uiCycleLen_g));
            }
            // continue
        }

        case kEplNmtMsPreOperational1:
        {
            PRINTF("%s(0x%X -> 0x%X) originating event = 0x%X\n",
                   __func__,
                   pEventArg_p->m_NmtStateChange.m_OldNmtState,
                   pEventArg_p->m_NmtStateChange.m_NewNmtState,
                   pEventArg_p->m_NmtStateChange.m_NmtEvent);

            // continue
        }

        case kEplNmtGsInitialising:
        case kEplNmtGsResetApplication:
        case kEplNmtMsNotActive:
        case kEplNmtCsNotActive:
        case kEplNmtCsPreOperational1:
        {
            break;
        }

        case kEplNmtCsOperational:
        case kEplNmtMsOperational:
        {
            break;
        }

        default:
        {
            break;
        }
        }

        break;
    }

    case kEplApiEventCriticalError:
    case kEplApiEventWarning:
    {   // error or warning occurred within the stack or the application
        // on error the API layer stops the NMT state machine

        PRINTF("%s(Err/Warn): Source=%02X EplError=0x%03X",
               __func__,
               pEventArg_p->m_InternalError.m_EventSource,
               pEventArg_p->m_InternalError.m_EplError);
        // check additional argument
        switch (pEventArg_p->m_InternalError.m_EventSource)
        {
        case kEplEventSourceEventk:
        case kEplEventSourceEventu:
        {   // error occurred within event processing
            // either in kernel or in user part
            PRINTF(" OrgSource=%02X\n", pEventArg_p->m_InternalError.m_Arg.m_EventSource);
            break;
        }

        case kEplEventSourceDllk:
        {   // error occurred within the data link layer (e.g. interrupt processing)
            // the DWORD argument contains the DLL state and the NMT event
            PRINTF(" val=%lX\n", (ULONG) pEventArg_p->m_InternalError.m_Arg.m_dwArg);
            break;
        }

        case kEplEventSourceObdk:
        case kEplEventSourceObdu:
        {   // error occurred within OBD module
            // either in kernel or in user part
            PRINTF(" Object=0x%04X/%u\n", pEventArg_p->m_InternalError.m_Arg.m_ObdError.m_uiIndex, pEventArg_p->m_InternalError.m_Arg.m_ObdError.m_uiSubIndex);
            break;
        }

        default:
        {
            PRINTF("\n");
            break;
        }
        }
        break;
    }

    case kEplApiEventHistoryEntry:
    {   // new history entry

        if ((pEventArg_p->m_ErrHistoryEntry.m_wErrorCode == EPL_E_DLL_CYCLE_EXCEED_TH)
                && (IS_FD_VALID(hAppFdTracingEnabled_g)))
        {
            mm_segment_t    old_fs;
            loff_t          pos;
            ssize_t         iRet;

            old_fs = get_fs();
            set_fs(KERNEL_DS);

            pos = hAppFdTracingEnabled_g->f_pos;
            iRet = vfs_write(hAppFdTracingEnabled_g, "0", 1, &pos);
            hAppFdTracingEnabled_g->f_pos = pos;
        }

        PRINTF("%s(HistoryEntry): Type=0x%04X Code=0x%04X (0x%02X %02X %02X %02X %02X %02X %02X %02X)\n",
               __func__,
               pEventArg_p->m_ErrHistoryEntry.m_wEntryType,
               pEventArg_p->m_ErrHistoryEntry.m_wErrorCode,
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[0],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[1],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[2],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[3],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[4],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[5],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[6],
               (WORD) pEventArg_p->m_ErrHistoryEntry.m_abAddInfo[7]);
        break;
    }

    case kEplApiEventNode:
    {
        // check additional argument
        switch (pEventArg_p->m_Node.m_NodeEvent)
        {
        case kEplNmtNodeEventCheckConf:
        {
            PRINTF("%s(Node=0x%X, CheckConf)\n", __func__, pEventArg_p->m_Node.m_uiNodeId);
            break;
        }

        case kEplNmtNodeEventUpdateConf:
        {
            PRINTF("%s(Node=0x%X, UpdateConf)\n", __func__, pEventArg_p->m_Node.m_uiNodeId);
            break;
        }

        case kEplNmtNodeEventNmtState:
        {
            PRINTF("%s(Node=0x%X, NmtState=0x%X)\n", __func__, pEventArg_p->m_Node.m_uiNodeId, pEventArg_p->m_Node.m_NmtState);
            break;
        }

        case kEplNmtNodeEventError:
        {
            PRINTF("%s (Node=0x%X): Error = %s (0x%.4X)\n", __func__,
                   pEventArg_p->m_Node.m_uiNodeId,
                   EplGetEmergErrCodeStr(pEventArg_p->m_Node.m_wErrorCode),
                   pEventArg_p->m_Node.m_wErrorCode);
            break;
        }

        case kEplNmtNodeEventFound:
        {
            PRINTF("%s(Node=0x%X, Found)\n", __func__, pEventArg_p->m_Node.m_uiNodeId);
            break;
        }

        default:
        {
            break;
        }
        }
        break;
    }

#if (((EPL_MODULE_INTEGRATION) & (EPL_MODULE_CFM)) != 0)
    case kEplApiEventCfmProgress:
    {
        PRINTF("%s(Node=0x%X, CFM-Progress: Object 0x%X/%u, ", __func__, pEventArg_p->m_CfmProgress.m_uiNodeId, pEventArg_p->m_CfmProgress.m_uiObjectIndex, pEventArg_p->m_CfmProgress.m_uiObjectSubIndex);
        PRINTF("%lu/%lu Bytes", (ULONG) pEventArg_p->m_CfmProgress.m_dwBytesDownloaded, (ULONG) pEventArg_p->m_CfmProgress.m_dwTotalNumberOfBytes);
        if ((pEventArg_p->m_CfmProgress.m_dwSdoAbortCode != 0)
                || (pEventArg_p->m_CfmProgress.m_EplError != kEplSuccessful))
        {
            PRINTF(" -> SDO Abort=0x%lX, Error=0x%X)\n", (unsigned long) pEventArg_p->m_CfmProgress.m_dwSdoAbortCode,
                   pEventArg_p->m_CfmProgress.m_EplError);
        }
        else
        {
            PRINTF(")\n");
        }
        break;
    }

    case kEplApiEventCfmResult:
    {
        switch (pEventArg_p->m_CfmResult.m_NodeCommand)
        {
        case kEplNmtNodeCommandConfOk:
        {
            PRINTF("%s(Node=0x%X, ConfOk)\n", __func__, pEventArg_p->m_CfmResult.m_uiNodeId);
            break;
        }

        case kEplNmtNodeCommandConfErr:
        {
            PRINTF("%s(Node=0x%X, ConfErr)\n", __func__, pEventArg_p->m_CfmResult.m_uiNodeId);
            break;
        }

        case kEplNmtNodeCommandConfReset:
        {
            PRINTF("%s(Node=0x%X, ConfReset)\n", __func__, pEventArg_p->m_CfmResult.m_uiNodeId);
            break;
        }

        case kEplNmtNodeCommandConfRestored:
        {
            PRINTF("%s(Node=0x%X, ConfRestored)\n", __func__, pEventArg_p->m_CfmResult.m_uiNodeId);
            break;
        }

        default:
        {
            PRINTF("%s(Node=0x%X, CfmResult=0x%X)\n", __func__, pEventArg_p->m_CfmResult.m_uiNodeId, pEventArg_p->m_CfmResult.m_NodeCommand);
            break;
        }
        }
        break;
    }
#endif

    default:
        break;
    }

    return EplRet;
}
static void hifi_dump_dsp(DUMP_DSP_INDEX index)
{
	int ret = 0;

	mm_segment_t fs = 0;
	struct file *fp = NULL;
	int file_flag = O_RDWR;
	struct kstat file_stat;
	int write_size = 0;
	unsigned int err_no = 0xFFFFFFFF;

	char tmp_buf[64] = {0};
	unsigned long tmp_len = 0;
	struct rtc_time cur_tm;
	struct timespec now;

	char  path_name[HIFI_DUMP_FILE_NAME_MAX_LEN] = {0};
	char* file_name		= s_dsp_dump_info[index].file_name;
	char* data_addr		= NULL;
	unsigned int data_len = s_dsp_dump_info[index].data_len;

	char* is_panic		= "i'm panic.\n";
	char* is_exception	= "i'm exception.\n";
	char* not_panic		= "i'm ok.\n";

	if ((index != NORMAL_LOG) && (index != PANIC_LOG) && g_om_data.is_watchdog_coming) {
		logi("watchdog is coming,so don't dump %s\n", file_name);
		return;
	}

	memset(path_name, 0, HIFI_DUMP_FILE_NAME_MAX_LEN);

	if (rdr_nv_get_value(RDR_NV_HIFI) != 1) {
		loge("do not save hifi log in nv config \n");
		return;
	}

	if (down_interruptible(&g_om_data.dsp_dump_sema) < 0) {
		loge("acquire the semaphore error.\n");
		return;
	}

	IN_FUNCTION;

	hifi_get_log_signal();

	g_om_data.dsp_log_addr = (char*)ioremap_wc(DRV_DSP_UART_TO_MEM, DRV_DSP_UART_TO_MEM_SIZE);
	if (NULL == g_om_data.dsp_log_addr) {
		loge("dsp log ioremap_wc fail.\n");
		goto END;
	}

	s_dsp_dump_info[NORMAL_LOG].data_addr = g_om_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE;
	s_dsp_dump_info[PANIC_LOG].data_addr  = g_om_data.dsp_log_addr + DRV_DSP_UART_TO_MEM_RESERVE_SIZE;

	if (index == OCRAM_BIN)
	{
		s_dsp_dump_info[index].data_addr = (unsigned char*)ioremap_wc(HIFI_OCRAM_BASE_ADDR, HIFI_IMAGE_OCRAMBAK_SIZE);
	}
	if (index == TCM_BIN)
	{
		s_dsp_dump_info[index].data_addr = (unsigned char*)ioremap_wc(HIFI_TCM_BASE_ADDR, HIFI_IMAGE_TCMBAK_SIZE);
	}

	if (NULL == s_dsp_dump_info[index].data_addr) {
		loge("dsp log ioremap_wc fail.\n");
		goto END;
	}

	data_addr = s_dsp_dump_info[index].data_addr;

	fs = get_fs();
	set_fs(KERNEL_DS);

	ret = hifi_create_dir(HIFI_LOG_PATH_PARENT);
	if (0 != ret) {
		goto END;
	}

	ret = hifi_create_dir(HIFI_LOG_PATH);
	if (0 != ret) {
		goto END;
	}

	snprintf(path_name, HIFI_DUMP_FILE_NAME_MAX_LEN, "%s%s%s", HIFI_LOG_PATH, g_om_data.cur_dump_time, "/");

	ret = hifi_create_dir(path_name);
	if (0 != ret) {
		goto END;
	}

	snprintf(path_name, HIFI_DUMP_FILE_NAME_MAX_LEN, "%s%s", path_name, file_name);

	ret = vfs_stat(path_name, &file_stat);
	if (ret < 0) {
		logi("there isn't a dsp log file:%s, and need to create.\n", path_name);
		file_flag |= O_CREAT;
	}

	fp = filp_open(path_name, file_flag, 0664);
	if (IS_ERR(fp)) {
		loge("open file fail: %s.\n", path_name);
		fp = NULL;
		goto END;
	}

	/*write from file start*/
	vfs_llseek(fp, 0, SEEK_SET);

	/*write file head*/
	if (DUMP_DSP_LOG == s_dsp_dump_info[index].dump_type) {
		/*write dump log time*/
		now = current_kernel_time();
		rtc_time_to_tm(now.tv_sec, &cur_tm);
		memset(tmp_buf, 0, 64);
		tmp_len = sprintf(tmp_buf, "%04d-%02d-%02d %02d:%02d:%02d.\n",
								cur_tm.tm_year+1900, cur_tm.tm_mon+1,
								cur_tm.tm_mday, cur_tm.tm_hour,
								cur_tm.tm_min, cur_tm.tm_sec);
		vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos);

		/*write exception no*/
		memset(tmp_buf, 0, 64);
		err_no = (unsigned int)(*(g_om_data.dsp_exception_no));
		if (err_no != 0xFFFFFFFF) {
			tmp_len = sprintf(tmp_buf, "the exception no: %u.\n", err_no);
		} else {
			tmp_len = sprintf(tmp_buf, "%s", "hifi is fine, just dump log.\n");
		}
		vfs_write(fp, tmp_buf, tmp_len, &fp->f_pos);

		/*write error type*/
		if (0xdeadbeaf == *g_om_data.dsp_panic_mark) {
			vfs_write(fp, is_panic, strlen(is_panic), &fp->f_pos);
		} else if(0xbeafdead == *g_om_data.dsp_panic_mark){
			vfs_write(fp, is_exception, strlen(is_exception), &fp->f_pos);
		} else {
			vfs_write(fp, not_panic, strlen(not_panic), &fp->f_pos);
		}
	}

	/*write dsp info*/
	if((write_size = vfs_write(fp, data_addr, data_len, &fp->f_pos)) < 0) {
		loge("write file fail.\n");
	}

	logi("write file size: %d.\n", write_size);

END:
	if (fp) {
		filp_close(fp, 0);
	}
	set_fs(fs);

	if (NULL != g_om_data.dsp_log_addr) {
		iounmap(g_om_data.dsp_log_addr);
		g_om_data.dsp_log_addr = NULL;
	}

	if((index == OCRAM_BIN || index == TCM_BIN) && (NULL != s_dsp_dump_info[index].data_addr))
	{
		iounmap(s_dsp_dump_info[index].data_addr);
		s_dsp_dump_info[index].data_addr = NULL;
	}

	hifi_release_log_signal();

	up(&g_om_data.dsp_dump_sema);
	OUT_FUNCTION;

	return;
}
Esempio n. 5
0
static int write_bootloader_message(char *cmd, int mode)
{
	struct file *filp;
	mm_segment_t oldfs;
	int ret = 0;
	loff_t pos = 2048L;  /* bootloader message offset in MISC.*/
#ifdef CONFIG_KERNEL_DEBUG_SEC
	int state;
#endif

	struct bootloader_message  bootmsg;

	memset(&bootmsg, 0, sizeof(struct bootloader_message));

	if (mode == REBOOT_MODE_RECOVERY) {
		strcpy(bootmsg.command, "boot-recovery");
#ifdef CONFIG_KERNEL_DEBUG_SEC
		reboot_mode = REBOOT_MODE_RECOVERY;
		kernel_sec_set_debug_level(KERNEL_SEC_DEBUG_LEVEL_LOW);
		state = 1;	/* Set USB path to AP */
		sec_set_param(param_index_usbsel, &state);
#endif
	} else if (mode == REBOOT_MODE_FASTBOOT)
		strcpy(bootmsg.command, "boot-fastboot");
	else if (mode == REBOOT_MODE_NORMAL)
		strcpy(bootmsg.command, "boot-reboot");
	else if (mode == REBOOT_MODE_FOTA)
		strcpy(bootmsg.command, "boot-fota");
	else if (mode == REBOOT_MODE_NONE)
		strcpy(bootmsg.command, "boot-normal");
	else
		strcpy(bootmsg.command, cmd);

	bootmsg.status[0] = (char) mode;


	filp = filp_open(MISC_DEVICE, O_WRONLY, 0);

	if (IS_ERR(filp)) {
		pr_info("failed to open MISC : '%s'.\n", MISC_DEVICE);
		return 0;
	}

	oldfs = get_fs();
	set_fs(KERNEL_DS);

	ret = vfs_write(filp, (const char *)&bootmsg,
			sizeof(struct bootloader_message), &pos);

	set_fs(oldfs);

	if (ret < 0)
		pr_info("failed to write on MISC\n");
	else
		pr_info("command : %s written on MISC\n", bootmsg.command);

	fput(filp);
	filp_close(filp, NULL);

	return ret;
}
Esempio n. 6
0
static int cat2(int argc, char *argv[])
{
    errval_t err;
    char *path;
    int ret;

    if(argc < 3) {
        printf("Usage: %s [input-files...] output-file\n", argv[0]);
        return 1;
    }

    /* Open output file creating it if it does not exist */
    path = vfs_path_mkabsolute(cwd, argv[argc - 1]);
    vfs_handle_t output_vh;
    err = vfs_create(path, &output_vh);
    free(path);
    if (err_is_fail(err)) {
        DEBUG_ERR(err, "error opening output file");
        return 1;
    }

    /* Open input files, read buffer and write to output file */
    for (int i = 1; i < argc - 1; i++) {
        uint8_t buf[1024];
        size_t size;
        vfs_handle_t input_vh;
        path = vfs_path_mkabsolute(cwd, argv[i]);
        err = vfs_open(path, &input_vh);
        free(path);
        if (err_is_fail(err)) {
            printf("%s: file not found\n", argv[i]);
            ret = 1;
            continue;
        }

        do {
            err = vfs_read(input_vh, buf, sizeof(buf), &size);
            if (err_is_fail(err)) {
                // XXX: Close any files that might be open
                DEBUG_ERR(err, "error reading file");
                return 1;
            }

            size_t output_size;
            err = vfs_write(output_vh, buf, size, &output_size);
            if (err_is_fail(err)) {
                // XXX: Close any files that might be open
                DEBUG_ERR(err, "error writing to output file");
                return 1;
            }
            if (output_size != size) {
                printf("Wanted to write %zu but only wrote %zu, aborting\n",
                       size, output_size);
                // XXX: Close any files that might be open
                return 1;
            }
        } while(size > 0);

        err = vfs_close(input_vh);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_close");
        }
    }

    err = vfs_close(output_vh);
    if (err_is_fail(err)) {
        DEBUG_ERR(err, "in vfs_close");
    }
    return ret;
}
static void last_kmsg_work_function(struct work_struct *dat)
{
	char *buffer;
	struct file *fp;
	struct file *fp_proc;
	struct file *fp_mounts;
	mm_segment_t old_fs;
	ssize_t result;
	//**************************************
	//mmcblk0p8 just for t30 branch
	//**************************************
	char mount_point[32] = "mmcblk0p8";
	#define PMC_RST_STATUS_WDT (1)
	#define PMC_RST_STATUS_SW   (3)

	printk(KERN_INFO "dump last_kmsg starting\n");

	buffer=kmalloc(SZ_1M, GFP_KERNEL);
	memset(buffer, 0, SZ_1M);

	if(!buffer){
		printk(KERN_INFO "last_kmsg_work_function:alloc buffer fail!\n");
		return;
	}

	if (last_kmsg_log_filename() < 0){
		printk(KERN_INFO "%s folder doesn't exist, and create fail !\n", DATA_LOGS);
		kfree(buffer);
		return ;
	}

	old_fs = get_fs();
	set_fs(KERNEL_DS);

	while(1)
	{
		fp_mounts = filp_open("/proc/mounts" , O_RDONLY, 0);
		if (PTR_ERR(fp_mounts) == -ENOENT)
		{
			printk(KERN_INFO "last_kmsg_work_function:open /proc/mounts fail!\n");
			msleep(1000);
		}
		else
		{
			vfs_read(fp_mounts, buffer, SZ_1M, &fp_mounts->f_pos);

			if(!strstr(buffer, mount_point))
			{
				printk(KERN_INFO "last_kmsg_work_function:mmcblk0p8 was not mounted yet!\n");
				filp_close(fp_mounts,NULL);
				msleep(1000);
			}
			else
			{
				break;
			}
		}
	}

	memset(buffer, 0, SZ_1M);

	last_kmsg_get_time();
	strcat(rd_log_file, rd_kernel_time);

	if (boot_reason==PMC_RST_STATUS_WDT)
	{
		strcat(rd_log_file,".log_wdt");
	}
	else if (boot_reason==PMC_RST_STATUS_SW )
	{
		strcat(rd_log_file,".log_SwReboot");
	}
	else
	{
		strcat(rd_log_file,".log_normal");
	}

	fp_proc = filp_open("/proc/last_kmsg" , O_RDONLY, 0);
	if (PTR_ERR(fp_proc) == -ENOENT)
	{
		printk(KERN_INFO "last_kmsg_work_function:last_kmsg is empty!\n");
		filp_close(fp_mounts,NULL);
		set_fs(old_fs);
		kfree(buffer);
		return ;
	}

	fp = filp_open(rd_log_file , O_APPEND | O_RDWR | O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
	if (PTR_ERR(fp) == -ENOENT)
	{
		printk(KERN_INFO "last_kmsg_work_function:open log file fail!\n");
		filp_close(fp_mounts,NULL);
		filp_close(fp_proc,NULL);
		set_fs(old_fs);
		kfree(buffer);
		return ;
	}

	result=vfs_read(fp_proc, buffer, SZ_1M, &fp_proc->f_pos);
	if( result < 0 )
	{
		printk(KERN_INFO "last_kmsg_work_function:read last_kmsg fail!\n");
	}
	else
	{
		result=vfs_write(fp, buffer, result, &fp->f_pos);
		if( result < 0 )
			printk(KERN_INFO "last_kmsg_work_function:write last_kmsg fail!\n");
	}

	filp_close(fp_mounts,NULL);
	filp_close(fp_proc,NULL);
	filp_close(fp,NULL);
	set_fs(old_fs);
	kfree(buffer);
	printk(KERN_INFO "last_kmsg file: %s\n", rd_log_file);
	return;

}
Esempio n. 8
0
//XXX: flags are ignored...
int vfsfd_open(const char *pathname, int flags)
{
    vfs_handle_t vh;
    errval_t err;

    char *path = vfs_path_mkabs(pathname);
    assert(path != NULL);

    // If O_CREAT was given, we use vfs_create()
    if(flags & O_CREAT) {
        // If O_EXCL was also given, we check whether we can open() first
        if(flags & O_EXCL) {
            err = vfs_open(path, &vh);
            if(err_is_ok(err)) {
                vfs_close(vh);
                errno = EEXIST;
                return -1;
            }
            assert(err_no(err) == FS_ERR_NOTFOUND);
        }

        err = vfs_create(path, &vh);
    } else {
        // Regular open()
        err = vfs_open(path, &vh);
    }

    free(path);
    if (err_is_fail(err)) {
        VFSFD_DEBUG("open('%s') failed\n", pathname);

        switch(err_no(err)) {
        case FS_ERR_NOTFOUND:
            errno = ENOENT;
            break;

        default:
#ifdef VFSFD_DEBUG_ENABLED
            DEBUG_ERR(err, "vfs_open");
#endif
            break;
        }

        return -1;
    }

    struct fdtab_entry e = {
        .type = FDTAB_TYPE_FILE,
        .handle = vh,
        .epoll_fd = -1,
    };
    int fd = fdtab_alloc(&e);
    VFSFD_DEBUG("open(%s) as fd %d\n", pathname, fd);
    if (fd < 0) {
        vfs_close(vh);
        return -1;
    } else {
        return fd;
    }
}


int vfsfd_read(int fd, void *buf, size_t len)
{
    struct fdtab_entry *e = fdtab_get(fd);
    size_t retlen = 0;
    switch(e->type) {
    case FDTAB_TYPE_FILE:
        {
            errval_t err = vfs_read((vfs_handle_t)e->handle, buf, len, &retlen);
            VFSFD_DEBUG("%d : read(%d, %d) = %lu\n", disp_get_domain_id(), fd, len, retlen);
            if (err_is_fail(err)) {
                DEBUG_ERR(err, "error in vfs_read");
                return -1;
            }
        }
        break;

    case FDTAB_TYPE_STDIN:
        retlen = terminal_read((char *)buf, len);
        break;

    case FDTAB_TYPE_STDOUT:
    case FDTAB_TYPE_STDERR:
    case FDTAB_TYPE_AVAILABLE:
    default:
        return -1;
    }

    return retlen;
}

int vfsfd_write(int fd, const void *buf, size_t len)
{
    struct fdtab_entry *e = fdtab_get(fd);
    if (e->type == FDTAB_TYPE_AVAILABLE) {
        return -1;
    }

    size_t retlen = 0;

    switch(e->type) {
    case FDTAB_TYPE_FILE:
        {
            errval_t err = vfs_write((vfs_handle_t)e->handle, buf, len, &retlen);
            VFSFD_DEBUG("write(%d, %d) = %lu\n", fd, len, retlen);
            if (err_is_fail(err)) {
                DEBUG_ERR(err, "error in vfs_write");
                return -1;
            }
        }
        break;

    case FDTAB_TYPE_STDOUT:
        /* only support writting to terminal */
        retlen = terminal_write((const char*) buf, len);
        break;

    case FDTAB_TYPE_STDERR:
        retlen = terminal_write((const char*) buf, len);
        break;

    case FDTAB_TYPE_STDIN:
    case FDTAB_TYPE_AVAILABLE:
    default:
        return -1;
    }

    return retlen;
}

int vfsfd_close(int fd)
{
    errval_t err;
    struct fdtab_entry *e = fdtab_get(fd);
    if (e->type == FDTAB_TYPE_AVAILABLE) {
        return -1;
    }

    VFSFD_DEBUG("close(%d)\n", fd);

    switch(e->type) {
    case FDTAB_TYPE_FILE:
        err = vfs_close((vfs_handle_t)e->handle);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "error in vfs_close");
            return -1;
        }
        break;

    case FDTAB_TYPE_STDIN:
    case FDTAB_TYPE_STDOUT:
    case FDTAB_TYPE_STDERR:
        // XXX: Should call fclose() when closing last FD
        break;

    default:
        return -1;
    } // end switch

    fdtab_free(fd);
    return 0;
}
Esempio n. 9
0
/* Write contents of flash_memory to nand emu file */
int emu_write_mem_to_file(void)
{
	mm_segment_t fs;
	struct file *nef_filp = NULL;
	struct inode *inode = NULL;
	loff_t nef_size = 0;
	loff_t tmp_file_offset, file_offset;
	ssize_t nwritten;
	int i, rc = -EINVAL;

	nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
		       __FILE__, __LINE__, __func__);

	fs = get_fs();
	set_fs(get_ds());

	nef_filp = filp_open("/root/nand_emu_file", O_RDWR | O_LARGEFILE, 0);
	if (IS_ERR(nef_filp)) {
		printk(KERN_ERR "filp_open error: "
		       "Unable to open nand emu file!\n");
		return PTR_ERR(nef_filp);
	}

	if (nef_filp->f_path.dentry) {
		inode = nef_filp->f_path.dentry->d_inode;
	} else {
		printk(KERN_ERR "Invalid " "nef_filp->f_path.dentry value!\n");
		goto out;
	}

	nef_size = i_size_read(inode->i_mapping->host);
	if (nef_size <= 0) {
		printk(KERN_ERR "Invalid "
		       "nand emu file size: 0x%llx\n", nef_size);
		goto out;
	} else {
		nand_dbg_print(NAND_DBG_DEBUG, "nand emu file size: "
			       "%lld\n", nef_size);
	}

	file_offset = 0;
	for (i = 0; i < GLOB_LLD_BLOCKS * GLOB_LLD_PAGES; i++) {
		tmp_file_offset = file_offset;
		nwritten = vfs_write(nef_filp,
				     (char __user *)flash_memory[i],
				     GLOB_LLD_PAGE_SIZE, &tmp_file_offset);
		if (nwritten < GLOB_LLD_PAGE_SIZE) {
			printk(KERN_ERR "%s, Line %d - "
			       "nand emu file partial write: "
			       "%d bytes\n", __FILE__, __LINE__, (int)nwritten);
			goto out;
		}
		file_offset += GLOB_LLD_PAGE_SIZE;
	}
	rc = 0;

out:
	filp_close(nef_filp, current->files);
	set_fs(fs);
	return rc;
}
int concat_files(struct file *in_filp1, struct file *in_filp2,
		 struct file *out_filp)
{
	mm_segment_t oldfs;
	int bytes_read = 0, bytes_write = 0;
	char *read_buf = NULL;
	int infile_size = 0;
	int ret = 0;

	/* Start offset */
	in_filp1->f_pos = 0;
	in_filp2->f_pos = 0;
	out_filp->f_pos = 0;

	/* Get the size of input file */
	infile_size = in_filp1->f_inode->i_size;

	/* Allocate memory for read buffer, used for reading data from input file in blocks of PAGE_SIZE*/
	read_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!read_buf) {
		printk(KERN_ERR	"Line no.:[%d] ERROR!! No memory allocated to read_buf buffer\n", __LINE__);
		ret = -ENOMEM;
		goto out;
	}

	/* Run loop till end of the input file 1 */
	while (in_filp1->f_pos < infile_size) {
		/* Read data in blocks of PAGE_SIZE from input file 1 */
		oldfs = get_fs();
		set_fs(KERNEL_DS);
		memset(read_buf, 0, PAGE_SIZE);
		bytes_read = vfs_read(in_filp1, read_buf, PAGE_SIZE,
				      &in_filp1->f_pos);
		set_fs(oldfs);

		/* Check if there is error in reading data from input file 1 */
		if (bytes_read < 0) {
			printk(KERN_ERR	"Line no.:[%d] ERROR in reading data from input file 1, bytes_read: %d\n", __LINE__, bytes_read);
			ret = bytes_read;
			goto out;
		}

		/* Write data block in output file */
		oldfs = get_fs();
		set_fs(KERNEL_DS);
		bytes_write = vfs_write(out_filp, read_buf, bytes_read,
					&out_filp->f_pos);
		set_fs(oldfs);

		/* Check if there is error in writing data to o/p file */
		if (bytes_write < bytes_read) {
			printk(KERN_ERR	"Line no.:[%d] ERROR in writing data to output file, bytes_write: %d\n", __LINE__, bytes_write);
			ret = bytes_write;
			goto out;
		}
	}

	infile_size = in_filp2->f_inode->i_size;
	/* Run loop till end of the input file 2 */
	while (in_filp2->f_pos < infile_size) {
		/* Read data in blocks of PAGE_SIZE from input file 2 */
		oldfs = get_fs();
		set_fs(KERNEL_DS);
		memset(read_buf, 0, PAGE_SIZE);
		bytes_read = vfs_read(in_filp2, read_buf, PAGE_SIZE,
				      &in_filp2->f_pos);
		set_fs(oldfs);

		/* Check if there is error in reading data from input file 2 */
		if (bytes_read < 0) {
			printk(KERN_ERR	"Line no.:[%d] ERROR in reading data from input file 2, bytes_read: %d\n", __LINE__, bytes_read);
			ret = bytes_read;
			goto out;
		}

		/* Write data block in output file */
		oldfs = get_fs();
		set_fs(KERNEL_DS);
		bytes_write = vfs_write(out_filp, read_buf, bytes_read,
					&out_filp->f_pos);
		set_fs(oldfs);

		/* Check if there is error in writing data to o/p file */
		if (bytes_write < bytes_read) {
			printk(KERN_ERR	"Line no.:[%d] ERROR in writing data to output file, bytes_write: %d\n", __LINE__, bytes_write);
			ret = bytes_write;
			goto out;
		}
	}

out:
	kfree(read_buf);
	return ret;
}
Esempio n. 11
0
static ssize_t  phy_mem_store(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count)
{

	char my_buf[1024];
	memset(my_buf, 0, sizeof(my_buf));
	strcpy(my_buf, buf);


	char *out_file = NULL;
	char *p = my_buf;
	char *tmp = p;
	while (*p++ != ' ');
    start_addr = simple_strtoul(tmp, my_buf+count, 10);

	
	tmp = p;
	while (*p++ != ' ');
    mem_len = simple_strtoul(tmp, my_buf+count, 10);


	out_file = p;
	my_buf[count-1] = '\0';
	
	printk("phy_mem_store, start_addr = %u, mem_len = %u, out_file = [%s]\n", start_addr, mem_len, out_file);

    char *line_addr = NULL;
    line_addr = ioremap_nocache(start_addr, mem_len);
    if (line_addr == NULL) {
        printk("ioremap_nocache() failed\n");
        return 0;
    }




	/////////////////////////////////////////////////////////
    mm_segment_t oldfs;
    oldfs = get_fs();
    set_fs(KERNEL_DS);

	struct file *filp = filp_open(out_file, O_CREAT|O_RDWR, 0);
	if (filp == NULL)printk("filp open failed\n");

	int len = mem_len;
	loff_t pos = 0;
	while (len > 0) {
		int n = vfs_write(filp, (char*)line_addr, len, &pos);
		len -= n;
		printk("write %d bytes\n", n);
	}

	
	set_fs(oldfs);	

    filp_close(filp, NULL);
	/////////////////////////////////////////////////////////

	iounmap(line_addr);

	return count;
}
Esempio n. 12
0
static void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
{
	bool cont = true;
	
	/*
	 * The connection was opened via the IPC_CONNECT_ME_TO call.
	 * This call needs to be answered.
	 */
	async_answer_0(iid, EOK);
	
	while (cont) {
		ipc_call_t call;
		ipc_callid_t callid = async_get_call(&call);
		
		if (!IPC_GET_IMETHOD(call))
			break;
		
		switch (IPC_GET_IMETHOD(call)) {
		case VFS_IN_REGISTER:
			vfs_register(callid, &call);
			cont = false;
			break;
		case VFS_IN_MOUNT:
			vfs_mount(callid, &call);
			break;
		case VFS_IN_UNMOUNT:
			vfs_unmount(callid, &call);
			break;
		case VFS_IN_OPEN:
			vfs_open(callid, &call);
			break;
		case VFS_IN_CLOSE:
			vfs_close(callid, &call);
			break;
		case VFS_IN_READ:
			vfs_read(callid, &call);
			break;
		case VFS_IN_WRITE:
			vfs_write(callid, &call);
			break;
		case VFS_IN_SEEK:
			vfs_seek(callid, &call);
			break;
		case VFS_IN_TRUNCATE:
			vfs_truncate(callid, &call);
			break;
		case VFS_IN_FSTAT:
			vfs_fstat(callid, &call);
			break;
		case VFS_IN_STAT:
			vfs_stat(callid, &call);
			break;
		case VFS_IN_MKDIR:
			vfs_mkdir(callid, &call);
			break;
		case VFS_IN_UNLINK:
			vfs_unlink(callid, &call);
			break;
		case VFS_IN_RENAME:
			vfs_rename(callid, &call);
			break;
		case VFS_IN_SYNC:
			vfs_sync(callid, &call);
			break;
		case VFS_IN_DUP:
			vfs_dup(callid, &call);
			break;
		case VFS_IN_WAIT_HANDLE:
			vfs_wait_handle(callid, &call);
			break;
		case VFS_IN_MTAB_GET:
			vfs_get_mtab(callid, &call);
			break;
		default:
			async_answer_0(callid, ENOTSUP);
			break;
		}
	}
	
	/*
	 * Open files for this client will be cleaned up when its last
	 * connection fibril terminates.
	 */
}
Esempio n. 13
0
int wrapfs_write_end(struct file *file, struct address_space *mapping,
		loff_t pos, unsigned len, unsigned copied,
		struct page *page, void *fsdata)
{
	char *page_data = (char *)kmap(page);
	struct file *lower_file = wrapfs_lower_file(file);
	struct inode *inode = page->mapping->host;
	struct inode *lower_inode = NULL;
	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
	mm_segment_t old_fs;
	int err = 0;
#ifdef WRAPFS_CRYPTO
	struct inode *cur_inode = page->mapping->host;
	pgoff_t index = pos >> (PAGE_CACHE_SHIFT);
	loff_t cur_inode_size = cur_inode->i_size;
	pgoff_t cur_inode_last_index = cur_inode_size >> (PAGE_CACHE_SHIFT);
	unsigned int cur_inode_end_offset;
	loff_t extra_padding = pos - cur_inode_size;
	char *encrypted_buf = NULL;
	unsigned copied1 = copied;
	cur_inode_end_offset = cur_inode_size & (PAGE_CACHE_SIZE - 1);
#endif
	wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops,
				"");
	wrapfs_debug("");
	if (lower_file == NULL) {
		wrapfs_debug("lower_file is NULL!!\n");
		err = -EACCES;
		goto out;
	}

	wrapfs_debug("pos : %lld", pos);
	wrapfs_debug("from : %u", from);
	wrapfs_debug("copied : %u", copied);
	wrapfs_debug("lower_file->f_pos : %lld", lower_file->f_pos);
#ifdef WRAPFS_CRYPTO
	if (extra_padding > 0 && (cur_inode_last_index == index)) {
		copied = copied + pos - cur_inode_size;
		from = cur_inode_end_offset;
	}
	encrypted_buf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
	if (encrypted_buf == NULL) {
		wrapfs_debug("encrypted_buf is NULL!!");
		err = -ENOMEM;
		goto out;
	}
	err = my_encrypt(page_data, PAGE_CACHE_SIZE, encrypted_buf,
				PAGE_CACHE_SIZE,
				WRAPFS_SB(file->f_dentry->d_sb)->key,
				WRAPFS_CRYPTO_KEY_LEN);
	if (err < 0) {
		wrapfs_debug("Encrypt failed!!");
		err = -EINVAL;
		kfree(encrypted_buf);
		goto out;
	}
#endif
	lower_file->f_pos = page_offset(page) + from;

	if (!PageUptodate(page))
		SetPageUptodate(page);

	wrapfs_debug("pos : %lld", pos);
	wrapfs_debug("from : %u", from);
	wrapfs_debug("copied : %u", copied);
	wrapfs_debug("lower_file->f_pos : %lld", lower_file->f_pos);

	old_fs = get_fs();
	set_fs(KERNEL_DS);
#ifndef WRAPFS_CRYPTO
	err = vfs_write(lower_file, page_data + from, copied,
				&lower_file->f_pos);
#else
	err = vfs_write(lower_file, encrypted_buf + from, copied,
				&lower_file->f_pos);
	/* If zeroes need to be placed, then err exceeds copied.
	 * In this case, we need to make err=copied1 to avoid oops in iov_iter
	 */
	if (err > 0 && extra_padding > 0)
		err = copied1;
#endif
	wrapfs_debug("err : %d", err);
	set_fs(old_fs);
	if (err < 0) {
		wrapfs_debug("vfs_write error : %d!!\n", err);
		err = -EINVAL;
#ifdef WRAPFS_CRYPTO
		kfree(encrypted_buf);
#endif
		goto out;
	}

	lower_inode = lower_file->f_path.dentry->d_inode;
	if (!lower_inode)
		lower_inode = wrapfs_lower_inode(inode);
	BUG_ON(!lower_inode);
	fsstack_copy_inode_size(inode, lower_inode);
	fsstack_copy_attr_times(inode, lower_inode);

out:
	kunmap(page);
	unlock_page(page);
	page_cache_release(page);
	wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops,
					"err : %d", err);
	return err;
}
Esempio n. 14
0
/* I have followed the behavior from ecryptfs. write_begin sets up the page.
 * for writing. Following changes are made :
 * 1. If Encrypt is not enabled, then just grab the page and set it up for
 *    write_begin. It is almost similar to ecryptfs. When we seek to a position
 *    after EOF and write, then the copied bytes are adjusted accordingly and
 *    passed. For example, if the file contains 2000 bytes and if we write
 *    1000 bytes from 3000th position(by lseeking), then from contains 3000 and
 *    copied contains 1000.  So we can directly copy 1000 bytes to lower file.
 * 2. When Encrypt is enabled, three cases are possible which are commented
 *    below. We must handle zero bytes cases explicitly.
 */
int wrapfs_write_begin(struct file *file, struct address_space *mapping,
		loff_t pos, unsigned len, unsigned flags,
		struct page **pagep, void **fsdata)
{
	struct page *page;
	char *page_data;
	pgoff_t index;
	int err = 0;
	struct inode *cur_inode, *lower_inode;
	unsigned int offset = 0;

#ifdef WRAPFS_CRYPTO
	/* pgoff_t is unsigned long, loff_t is long long */
	loff_t cur_inode_size;
	pgoff_t cur_inode_last_index;
	unsigned int cur_inode_end_offset;
	unsigned int zero_count;
	char *page_data_zeros;
	struct page *page_to_zeros = NULL;
	pgoff_t tempindex;
	pgoff_t tempoffset;
	pgoff_t bytes_to_write;
	struct file *lower_file = wrapfs_lower_file(file);
	char *encrypted_buf;
	mm_segment_t old_fs;
#endif

	wrapfs_debug("");
	wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops,
				"");

	index = pos >> PAGE_CACHE_SHIFT;
	offset = pos & (PAGE_CACHE_SIZE - 1);
	wrapfs_debug("index : %lu, offset : %d\n", index, offset);

	page = grab_cache_page_write_begin(mapping, index, flags);
	if (!page) {
		wrapfs_debug("grab_cache_page_write_begin returned NULL!!");
		err = -ENOMEM;
		goto out;
	}
	page_data = (char *)kmap(page);
	*pagep = page;

	cur_inode = file->f_path.dentry->d_inode;
	if (cur_inode)
		lower_inode = wrapfs_lower_inode(cur_inode);

#ifdef WRAPFS_CRYPTO
	/* cur_inode* refers to the file's existing attributes */
	cur_inode_size = cur_inode->i_size;
	cur_inode_last_index = cur_inode_size >> (PAGE_CACHE_SHIFT);
	cur_inode_end_offset = cur_inode_size & (PAGE_CACHE_SIZE - 1);

	wrapfs_debug(
	"cur_inode->i_size : %lu, i_size_read(page->mapping->host) : %lu\n",
	(unsigned long)cur_inode->i_size,
	(unsigned long)i_size_read(page->mapping->host));

	if (index == cur_inode_last_index) {
		/* The page to write is same as last page in file */
		wrapfs_debug("");
		if (pos > cur_inode_size) {
			/* Need to fill zeroes upto pos,
			 * from cur_inode_size */
			wrapfs_debug("");
			zero_count = pos - cur_inode_size;
			memset(page_data + cur_inode_end_offset, 0x00,
				zero_count);
		} else if (pos == cur_inode_size) {
			wrapfs_debug("");
			/* Fine. Do a normal encryption in write_end */
		} else if (pos < cur_inode_size) {
			/* Fine. Do a normal encryption in write_end */
			wrapfs_debug("");

		}
	} else if (index < cur_inode_last_index) {
		/* The page to write is an intermediate file page.
		 * No special cases need to be handled here.
		 */
		wrapfs_debug("");
	} else if (index > cur_inode_last_index) {
		/* If we skip to a page more than the last page in file.
		 * Need to fill holes between cur_inode_last_index and index.
		 * First filling hole in the new index page upto offset.
		 */
		wrapfs_debug("");
		memset(page_data, 0x00, offset);
		tempoffset = cur_inode_end_offset;
		tempindex = cur_inode_last_index;
		lower_file->f_pos = cur_inode_size;
		encrypted_buf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
		if (encrypted_buf == NULL) {
			wrapfs_debug("kmalloc failed!!");
			err = -ENOMEM;
			goto out_holes;
		}
		/* Fill zeroes in page cur_inode_last_index from cur off to end
		 * Then fill all pages from (cur_inode_last_index + 1) to index
		 * These must also be encrypted and written to lower file here
		 * itself as they are not reflected in write_end.
		 */
		while (tempindex < index) {
			page_to_zeros =
			grab_cache_page_write_begin(cur_inode->i_mapping,
							tempindex, flags);
			if (page_to_zeros == NULL) {
				wrapfs_debug("grab_cache_page failed!!");
				kfree(encrypted_buf);
				err = -ENOMEM;
				goto out_holes;
			}
			page_data_zeros = (char *)kmap(page_to_zeros);
			bytes_to_write = PAGE_CACHE_SIZE - tempoffset;
			memset(page_data_zeros + tempoffset, 0x00,
				bytes_to_write);
			err = my_encrypt(page_data_zeros, PAGE_CACHE_SIZE,
				encrypted_buf,
				PAGE_CACHE_SIZE,
				WRAPFS_SB(file->f_dentry->d_sb)->key,
				WRAPFS_CRYPTO_KEY_LEN);
			if (err < 0) {
				wrapfs_debug("Encryption failed!!");
				kfree(encrypted_buf);
				err = -EINVAL;
				goto free_pages_holes;
			}
			flush_dcache_page(page_to_zeros);

			old_fs = get_fs();
			set_fs(KERNEL_DS);
			err = vfs_write(lower_file,
					encrypted_buf + tempoffset,
					bytes_to_write,
					&lower_file->f_pos);
			set_fs(old_fs);
free_pages_holes:
			kunmap(page_to_zeros);
			unlock_page(page_to_zeros);
			page_cache_release(page_to_zeros);
			if (err < 0) {
				kfree(encrypted_buf);
				goto out_holes;
			}
			err = 0;
			mark_inode_dirty_sync(cur_inode);
			tempoffset = 0;
			tempindex++;
		} /* while ends */
out_holes:
		if ((err < 0) && (page_to_zeros != NULL))
			ClearPageUptodate(page_to_zeros);
	}
#endif

out:
	if (page)
		kunmap(page);
	if (unlikely(err)) {
		unlock_page(page);
		page_cache_release(page);
		*pagep = NULL;
	}
	wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops,
				"err : %d", err);
	return err;
}
Esempio n. 15
0
void ssp_dump_task(struct work_struct *work)
{
#if CONFIG_SEC_DEBUG
	struct ssp_big *big;
	struct file *dump_file;
	struct ssp_msg *msg;
	char *buffer;
	char strFilePath[100];
	struct timeval cur_time;
	mm_segment_t fs;
	int buf_len, packet_len, residue;
	int iRet = 0, index = 0, iRetTrans = 0, iRetWrite = 0;

	big = container_of(work, struct ssp_big, work);
	ssp_errf("start ssp dumping (%d)(%d)",
		big->data->bMcuDumpMode, big->data->uDumpCnt);

	big->data->uDumpCnt++;
	wake_lock(&big->data->ssp_wake_lock);

	fs = get_fs();
	set_fs(get_ds());

	if (big->data->bMcuDumpMode == true) {
		do_gettimeofday(&cur_time);
#ifdef CONFIG_SENSORS_SSP_ENG
		snprintf(strFilePath, sizeof(strFilePath), "%s%d.dump",
			DUMP_FILE_PATH,	(int)cur_time.tv_sec);
		dump_file = filp_open(strFilePath,
				O_RDWR | O_CREAT | O_APPEND, 0660);
#else
		snprintf(strFilePath, sizeof(strFilePath), "%s.dump",
			DUMP_FILE_PATH);
		dump_file = filp_open(strFilePath,
				O_RDWR | O_CREAT | O_TRUNC, 0660);
#endif

		if (IS_ERR(dump_file)) {
			ssp_errf("Can't open dump file");
			set_fs(fs);
			iRet = PTR_ERR(dump_file);
			wake_unlock(&big->data->ssp_wake_lock);
			kfree(big);
			return;
		}
	} else {
		dump_file = NULL;
	}

	buf_len = big->length > DATA_PACKET_SIZE
			? DATA_PACKET_SIZE : big->length;
	buffer = kzalloc(buf_len, GFP_KERNEL);
	residue = big->length;

	while (residue > 0) {
		packet_len = residue > DATA_PACKET_SIZE
				? DATA_PACKET_SIZE : residue;

		msg = kzalloc(sizeof(*msg), GFP_KERNEL);
		msg->cmd = MSG2SSP_AP_GET_BIG_DATA;
		msg->length = packet_len;
		msg->options = AP2HUB_READ | (index++ << SSP_INDEX);
		msg->data = big->addr;
		msg->buffer = buffer;
		msg->free_buffer = 0;

		iRetTrans = ssp_spi_sync(big->data, msg, 1000);
		if (iRetTrans != SUCCESS) {
			ssp_errf("Fail to receive data %d (%d)",
				iRetTrans, residue);
			break;
		}

		if (big->data->bMcuDumpMode == true) {
			iRetWrite = vfs_write(dump_file, (char __user *)buffer,
						packet_len, &dump_file->f_pos);
			if (iRetWrite < 0) {
				ssp_errf("Can't write dump to file");
				break;
			}
		}
		residue -= packet_len;
	}

	if (big->data->bMcuDumpMode == true) {
		if (iRetTrans != SUCCESS || iRetWrite < 0) { /* error case */
			char FAILSTRING[100];
			snprintf(FAILSTRING, sizeof(FAILSTRING),
				"FAIL OCCURED(%d)(%d)(%d)", iRetTrans,
				iRetWrite, big->length);
			vfs_write(dump_file, (char __user *)FAILSTRING,
					strlen(FAILSTRING), &dump_file->f_pos);
		}

		filp_close(dump_file, current->files);
	}

	big->data->bDumping = false;

	set_fs(fs);

	wake_unlock(&big->data->ssp_wake_lock);
	kfree(buffer);
	kfree(big);
#endif
	ssp_errf("done");
}
void _write_time_log(char *filename, char *data, int data_include)
{
	struct file *file;
	loff_t pos = 0;
	char *fname = NULL;
	char time_string[64] = {0};
	struct timespec my_time;
	struct tm my_date;

	mm_segment_t old_fs = get_fs();

	my_time = __current_kernel_time();
	time_to_tm(my_time.tv_sec, sys_tz.tz_minuteswest * 60 * (-1), &my_date);
	snprintf(time_string,
			sizeof(time_string),
			"%02d-%02d %02d:%02d:%02d.%03lu\n\n",
			my_date.tm_mon + 1,
			my_date.tm_mday,
			my_date.tm_hour,
			my_date.tm_min,
			my_date.tm_sec,
			(unsigned long) my_time.tv_nsec / 1000000);

	set_fs(KERNEL_DS);

	if (filename == NULL) {
		switch (mfts_mode) {
		case 0:
			if (factory_boot)
				fname = "/data/logger/touch_self_test.txt";
			else
				fname = "/sdcard/touch_self_test.txt";
			break;
		case 1:
			fname = "/data/logger/touch_self_test_mfts_folder.txt";
			break;
		case 2:
			fname = "/data/logger/touch_self_test_mfts_flat.txt";
			break;
		case 3:
			fname = "/data/logger/touch_self_test_mfts_curved.txt";
			break;
		default:
			TOUCH_I("%s : not support mfts_mode\n", __func__);
			break;
		}
	} else {
		fname = filename;
	}

	if (fname) {
		file = filp_open(fname,
			O_WRONLY|O_CREAT|O_APPEND, 0666);
		sys_chmod(fname, 0666);
	} else {
		TOUCH_E("%s : fname is NULL, can not open FILE\n", __func__);
		return;
	}

	if (IS_ERR(file)) {
		TOUCH_I("%s :  Open file error [%s], err = %ld\n",
				__func__, fname, PTR_ERR(file));

		set_fs(old_fs);
		return;
	}

	vfs_write(file, time_string, strlen(time_string), &pos);
	filp_close(file, 0);

	set_fs(old_fs);
}
Esempio n. 17
0
int debug_crash_dump(struct ssp_data *data, char *pchRcvDataFrame, int iLength)
{
	struct timeval cur_time;
	char strFilePath[100];
	int iRetWrite = 0;
	unsigned char datacount = pchRcvDataFrame[1];
	unsigned int databodysize = iLength - 2;
	char *databody = &pchRcvDataFrame[2];
/*
	if(iLength != DEBUG_DUMP_DATA_SIZE)
	{
		ssp_errf("data length error(%d)", iLength);
		return FAIL;
	}
	else
		ssp_errf("length(%d)", databodysize);
*/
	ssp_errf("length(%d)", databodysize);

	if (data->bSspShutdown) {
		ssp_infof("ssp shutdown, stop dumping");
		return FAIL;
	}

	if (data->bMcuDumpMode == true)	{
		wake_lock(&data->ssp_wake_lock);

		if (data->realtime_dump_file == NULL) {
			backup_fs = get_fs();
			set_fs(get_ds());

			do_gettimeofday(&cur_time);

			snprintf(strFilePath, sizeof(strFilePath), "%s%d.dump",
				DEBUG_DUMP_FILE_PATH, (int)cur_time.tv_sec);
			data->realtime_dump_file = filp_open(strFilePath,
					O_RDWR | O_CREAT | O_APPEND, 0660);

			ssp_err("save_crash_dump : open file(%s)", strFilePath);

			if (IS_ERR(data->realtime_dump_file)) {
				ssp_errf("Can't open dump file");
				set_fs(backup_fs);
				data->realtime_dump_file = NULL;
				wake_unlock(&data->ssp_wake_lock);
				return FAIL;
			}
		}

		data->total_dump_size += databodysize;
		/* ssp_errf("total receive size(%d)", data->total_dump_size); */
		iRetWrite = vfs_write(data->realtime_dump_file,
					(char __user *)databody, databodysize,
					&data->realtime_dump_file->f_pos);
		if (iRetWrite < 0) {
			ssp_errf("Can't write dump to file");
			wake_unlock(&data->ssp_wake_lock);
			return FAIL;
		}

		if (datacount == DEBUG_DUMP_DATA_COMPLETE) {
			ssp_errf("close file(size=%d)", data->total_dump_size);
			filp_close(data->realtime_dump_file, current->files);
			set_fs(backup_fs);
			data->uDumpCnt++;
			data->total_dump_size = 0;
			data->realtime_dump_file = NULL;
			data->bDumping = false;
		}

		wake_unlock(&data->ssp_wake_lock);

		/*
		if(iLength == 2*1024)
			queue_refresh_task(data, 0);
		*/
	}

	return SUCCESS;
}
int _write_log(char *filename, char *data)
{
	struct file *file;
	loff_t pos = 0;
	int flags;
	char *fname = "/data/logger/touch_self_test.txt";
	char *fname_normal_boot = "/sdcard/touch_self_test.txt";
	char *fname_mfts_folder = "/data/logger/touch_self_test_mfts_folder.txt";
	char *fname_mfts_flat = "/data/logger/touch_self_test_mfts_flat.txt";
	char *fname_mfts_curved = "/data/logger/touch_self_test_mfts_curved.txt";
	int cap_file_exist = 0;

	if (f54_window_crack || f54_window_crack_check_mode == 0) {
		mm_segment_t old_fs = get_fs();
		set_fs(KERNEL_DS);
		flags = O_WRONLY | O_CREAT;

		if (filename == NULL) {
			flags |= O_APPEND;
			switch (mfts_mode) {
			case 0:
				if (factory_boot)
					filename = fname;
				else
					filename = fname_normal_boot;
				break;
			case 1:
				filename = fname_mfts_folder;
				break;
			case 2:
				filename = fname_mfts_flat;
				break;
			case 3:
				filename = fname_mfts_curved;
				break;
			default:
				TOUCH_I("%s : not support mfts_mode\n",
					__func__);
				break;
			}
		} else {
			cap_file_exist = 1;
		}

		if (filename) {
			file = filp_open(filename, flags, 0666);
			sys_chmod(filename, 0666);
		} else {
			TOUCH_E("%s : filename is NULL, can not open FILE\n",
				__func__);
			return -1;
		}

		if (IS_ERR(file)) {
			TOUCH_I("%s : ERR(%ld)  Open file error [%s]\n",
					__func__, PTR_ERR(file), filename);
			set_fs(old_fs);
			return PTR_ERR(file);
		}

		vfs_write(file, data, strlen(data), &pos);
		filp_close(file, 0);
		set_fs(old_fs);

		log_file_size_check(filename);
	}
	return cap_file_exist;
}
Esempio n. 19
0
static int cp(int argc, char *argv[])
{
    if(argc != 3) {
        printf("Usage: %s src dest\n", argv[0]);
        return 1;
    }

    static uint8_t buf[32768];
    size_t rsize, wsize;
    vfs_handle_t src = NULL, dst = NULL;
    errval_t err;
    int ret = 0;

    char *path = vfs_path_mkabsolute(cwd, argv[1]);
    err = vfs_open(path, &src);
    free(path);
    if (err_is_fail(err)) {
        printf("%s: %s\n", argv[1], err_getstring(err));
        return 1;
    }

    path = vfs_path_mkabsolute(cwd, argv[2]);
    err = vfs_create(path, &dst);
    free(path);
    if (err_is_fail(err)) {
        printf("%s: %s\n", argv[2], err_getstring(err));
        ret = 1;
        goto out;
    }

    err = vfs_truncate(dst, 0);
    if (err_is_fail(err)) {
        printf("truncate %s: %s\n", argv[2], err_getstring(err));
        ret = 1;
        goto out;
    }

    do {
        err = vfs_read(src, buf, sizeof(buf), &rsize);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "error reading file");
            ret = 1;
            goto out;
        }

        size_t wpos = 0;
        while (wpos < rsize) {
            err = vfs_write(dst, &buf[wpos], rsize - wpos, &wsize);
            if (err_is_fail(err) || wsize == 0) {
                DEBUG_ERR(err, "error writing file");
                ret = 1;
                goto out;
            }
            wpos += wsize;
        }
    } while(rsize > 0);

out:
    if (src != NULL) {
        err = vfs_close(src);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_close");
        }
    }

    if (dst != NULL) {
        err = vfs_close(dst);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_close");
        }
    }

    return ret;
}
Esempio n. 20
0
static loff_t
cr_splice_direct(struct file *src_filp, loff_t *src_ppos, struct file *dst_filp, loff_t count)
{
    struct splice_desc sd = {
	.len            = count,
	.total_len      = count,
	.flags          = 0,
	.pos            = *src_ppos,
	.u.file         = dst_filp,
    };

    long ret = splice_direct_to_actor(src_filp, &sd, cr_splice_actor);
    if (ret > 0) {
        *src_ppos += ret;
    }

    return ret;
}

#endif // HAVE_SPLICE_DIRECT_TO_ACTOR

/*
 * sendfile via do_generic_file_read()
 */
#if HAVE_4_ARG_DO_GENERIC_FILE_READ
  #define cr_generic_file_read(_f,_p,_d,_a) do_generic_file_read((_f),(_p),(_d),(_a))
#elif HAVE_5_ARG_DO_GENERIC_FILE_READ
  /* RH9 and RHEL add a non-block flag */
  #define cr_generic_file_read(_f,_p,_d,_a) do_generic_file_read((_f),(_p),(_d),(_a),0)
#endif
#ifdef cr_generic_file_read

#if HAVE_READ_DESCRIPTOR_T_BUF
  #define CR_ACTOR_DATA(_desc) ((_desc)->buf)
#elif HAVE_READ_DESCRIPTOR_T_ARG_DATA
  #define CR_ACTOR_DATA(_desc) ((_desc)->arg.data)
#else
  #error
#endif

static int
cr_sendfile_actor(read_descriptor_t *desc, struct page *page, unsigned long offset, unsigned long size)
{ 
    struct file *dst_filp = (struct file *)CR_ACTOR_DATA(desc);
    unsigned long bytes_left;
    char *addr, *p;
    mm_segment_t oldfs;
    ssize_t w = 0;

    if (size > desc->count) {
	size = desc->count;
    }
    bytes_left = size;

    oldfs = get_fs();
    set_fs(KERNEL_DS);

    addr = kmap(page);
    p = addr + offset;
    while (bytes_left) {
	w = vfs_write(dst_filp, p, bytes_left, &dst_filp->f_pos);
        if (w <= 0) break;
	p += w;
	bytes_left -= w;
    }
    kunmap(page);

    set_fs(oldfs);

    if (w < 0) {
	desc->error = w;
	w = 0;
    } else {
	w = size - bytes_left;
	desc->count -= w;
	desc->written += w;
    }

    return (int)w;
}

static loff_t
cr_sendfile_generic_read(struct file *dst_filp, struct file *src_filp, loff_t *src_ppos, loff_t count)
{
    read_descriptor_t desc;
    loff_t retval = 0;

    CR_ACTOR_DATA(&desc) = (char *)dst_filp; /* Yes, (char*) cast is needed */

#if BITS_PER_LONG == 32
    while (count) {
	/* Max 1GB chunks to avoid overflow (negative values) in desc.written */
	size_t chunk = ((count > 0x40000000) ? 0x40000000 : count);
        desc.count = chunk;
	desc.written = 0;
	desc.error = 0;
	cr_generic_file_read(src_filp, src_ppos, &desc, cr_sendfile_actor);
	if (!desc.written) return desc.error;
	if (desc.written != chunk) return -EIO;
	retval += chunk;
	count -= chunk;
    }
#elif BITS_PER_LONG == 64
    desc.count = count;
    desc.written = 0;
    desc.error = 0;
    cr_generic_file_read(src_filp, src_ppos, &desc, cr_sendfile_actor);
    retval = desc.written;
    if (!retval) {
	retval = desc.error;
    } else if (retval != count) {
	retval = -EIO;
    }
#else
      #error "Unknown BITS_PER_LONG"
#endif

    return retval;
}