summaryrefslogtreecommitdiff
path: root/recovery/ubootenv/uboot_env.cpp (plain)
blob: 887387ab1e27592b61a2eb4277234f9e748d1ef1
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5
6#include <errno.h>
7#include <sys/stat.h>
8#include <unistd.h>
9
10#include "ubootenv/Ubootenv.h"
11
12
13// ------------------------------------
14// for uboot environment variable operation
15// ------------------------------------
16
17
18int set_bootloader_env(const char* name, const char* value)
19{
20 Ubootenv *ubootenv = new Ubootenv();
21
22 char ubootenv_name[128] = {0};
23 const char *ubootenv_var = "ubootenv.var.";
24 sprintf(ubootenv_name, "%s%s", ubootenv_var, name);
25
26 if (ubootenv->updateValue(ubootenv_name, value)) {
27 fprintf(stderr,"could not set boot env\n");
28 return -1;
29 }
30
31 return 0;
32}
33
34char *get_bootloader_env(const char * name)
35{
36 Ubootenv *ubootenv = new Ubootenv();
37 char ubootenv_name[128] = {0};
38 const char *ubootenv_var = "ubootenv.var.";
39 sprintf(ubootenv_name, "%s%s", ubootenv_var, name);
40 return (char *)ubootenv->getValue(ubootenv_name);
41}
42
43
44int set_env_optarg(char * optarg)
45{
46 int ret = -1;
47 char *buffer = NULL, *name = NULL, *value = NULL;
48 if ((optarg == NULL) || (strlen(optarg) == 0)) {
49 printf("param error!\n");
50 return -1;
51 }
52
53 buffer = strdup(optarg);
54 if (!buffer) {
55 printf("strdup for buffer failed\n");
56 return -1;
57 }
58
59 name = strtok(buffer, "=");
60 if (strlen(name) == strlen(optarg)) {
61 printf("strtok for '=' failed\n");
62 goto END;
63 }
64
65 value = optarg + strlen(name) + 1;
66 if (strlen(name) == 0) {
67 printf("name is NULL\n");
68 goto END;
69 }
70
71 if (strlen(value) == 0) {
72 printf("value is NULL\n");
73 goto END;
74 }
75
76 ret = set_bootloader_env(name, value);
77 if (ret < 0) {
78 printf("set env :%s=%s failed", name, value);
79 }
80
81END:
82
83 if (buffer != NULL) {
84 free(buffer);
85 buffer = NULL;
86 }
87 return ret;
88}
89
90int get_env_optarg(const char * optarg)
91{
92 int ret = -1;
93
94 if (optarg == NULL) {
95 printf("param error!\n");
96 return -1;
97 }
98
99 char *env = get_bootloader_env(optarg);
100 if (env != NULL)
101 {
102 printf("get %s value:%s\n", optarg, env);
103 ret = 0;
104 }
105
106 return ret;
107}
108