⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 param.c

📁 SMDK2440 boot code, base on vivi
💻 C
字号:
/* * vivi/kernel/param.c * * Author:      Janghoon Lyu <nandy@mizi.com> * Created:     December 23, 2001 * Copyright:   MIZI Resarch, Inc. * Description: Simple vivi parameter management * * All Rights Reserved. * * $Date: 2003/12/12 21:24:34 $ * $Revision: 1.11 $ * $Id: param.c,v 1.11 2003/12/12 21:24:34 bushi Exp $ * */#include <config.h>#include <machine.h>#include <param.h>#include <command.h>#include <vstring.h>#include <types.h>#include <vmalloc.h>#ifdef CONFIG_VIVI_FS#include <vfs.h>#endifstatic vivi_param_t *vivi_params = NULL;static int nb_params = 0;static char *linux_cmd_line = NULL;extern char linux_cmd[];extern char default_vivi_parameters[];static voidset_linux_cmd_line(const char *cmd_line){	int l = strlen(cmd_line);	strncpy(linux_cmd_line, cmd_line, l);	linux_cmd_line[l] = '\0';}static intparse_options(void){	char *next;	char *line = default_vivi_parameters;	vivi_param_t *p = vivi_params;	int index;	index = 0;	next = line;	while ((line = next) != NULL) {		next = strchr(line, ' ');		if (next != NULL) *next++ = '\0';		strcpy((char *)p, line);		if (next != NULL) *(next-1) = ' ';		index++;		p++;		if (index == MAX_PARAMS) break;	}		return index;}static intget_default_param(void){	if (default_vivi_parameters == NULL)		return -1;	nb_params = parse_options();	set_linux_cmd_line(linux_cmd);	return 0;}char *get_param_str_p(const char *name){	int i;	char *v = NULL;	vivi_param_t *p = vivi_params;	for (i = 0; i < nb_params; i++, p++) {		if (strncmp(name, (char *)p, strlen(name)) == 0) {			v = strchr((char *)p, '='); v++;			break;		}	}	return v;}/* set parameter value */static voidset_param(const char *str){	int i, len;	char *v = NULL;	vivi_param_t *p = vivi_params;	for (i = 0; i < nb_params; i++, p++) {		v = strchr((char *)p, '=');		len = v - (char *)p;		if (strncmp(str, (char *)p, len) == 0) {			strcpy((char *)p, str);			*((char *)(p)+strlen(str)) = '\0';			return;		}	}	/* add new */	if (nb_params < MAX_PARAMS - 1) {		strcpy((char *)p, str);		*((char *)(p)+strlen(str)) = '\0';		nb_params ++;	} else {		printk("reatched maximum limitation (%d)\n", MAX_PARAMS);		}}/* display paramter table */static void display_param_tlb(void){	vivi_param_t *p = vivi_params;	int i;	for (i = 0; i < nb_params; i++, p++) {		printk("%s\n", p);	}	printk("linux_cmd=\"%s\"\n\n", linux_cmd_line);}/* * APIs for vivi */intget_param_ulong(const char *name, unsigned long *val){	char *v = get_param_str_p(name);	if (v == NULL) return -1;	*val = simple_strtoul(v, NULL, 0);	return 0;}char *get_param_str(const char *name, char *str){	char *v = get_param_str_p(name);	strcpy(str, v);	return v;}char *get_linux_cmd_line(void){	return linux_cmd_line;}voidadd_linux_cmd_line(const char *cmd){	char *c = linux_cmd_line;	if (linux_cmd_line != NULL)		strcat(c, cmd); }/* * commands for vivi */voiddo_set(int argc, const char **argv){	switch (argc) {	case 1:		display_param_tlb();		break;	case 2:		set_param(argv[1]);		return;		break;	case 3:		if (strncmp(argv[1], "linux_cmd", 9) == 0) {			set_linux_cmd_line(argv[2]);			return;		}		break;	}}#ifdef CONFIG_VIVI_FSvoiddo_save(void){	int fd;	int param_size = MAX_PARAMS * sizeof(vivi_param_t);	fd = vfs_open("param.cfg", VFS_O_CREAT);	if (fd < 0) goto fail;	if (vfs_write(fd, (void *)&nb_params, 4) != 4) {		goto fail;	}	if (vfs_write(fd, vivi_params, param_size) != param_size) {		goto fail;	}	vfs_close(fd);	fd = vfs_open("linuxcmd.cfg", VFS_O_CREAT);	if (fd < 0) goto fail;	if (vfs_write(fd, linux_cmd_line, 1024) != 1024) {		goto fail;	}	vfs_close(fd);	printk("saved paramters.\n");	return;fail:	printk("Can't save parameters.\n");	return;}#endifvoid do_param(int argc, const char **argv){	switch (argc) {	case 2:		if (strncmp(argv[1], "reset", 5) == 0) {			get_default_param();			return;		}#ifdef CONFIG_VIVI_FS		if (strncmp(argv[1], "save", 4) == 0) {			do_save();			return;		}#endif		break;	}}user_command_t param_cmd = {	"param",	do_param,	NULL};user_command_t set_cmd = {	"set",	do_set,	NULL};/* initialize vivi paramters */intinit_param(void){	int param_size = MAX_PARAMS * sizeof(vivi_param_t);	/* Allocate memory */	vivi_params = vmalloc(param_size);	linux_cmd_line = vmalloc(1024);#ifdef CONFIG_VIVI_FS	/* Try to find stored parameters */	{		int fd;		fd = vfs_open("param.cfg", VFS_O_RDWR);		if (fd < 0) goto try_default;		if (vfs_read(fd, (void *)&nb_params, 4) != 4) {			goto try_default;		}		if (vfs_read(fd, vivi_params, param_size) != param_size) {			goto try_default;		}		vfs_close(fd);		fd = vfs_open("linuxcmd.cfg", VFS_O_RDWR);		if (fd < 0) goto try_default;		if (vfs_read(fd, linux_cmd_line, 1024) != 1024) {			goto try_default;		}		vfs_close(fd);		goto done;try_default:		vfs_close(fd);	}#endif	/* Try to find built-in mtd_partitions */	if (get_default_param() == 0) {		printk("try to get default paramters.\n");		goto done;	}	return -1;done:	add_command(&param_cmd);	add_command(&set_cmd);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -