1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /* Add firmware update command to recovery script */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "edify/expr.h"
28 #include "update_fw.h"
29 
firmware_update(const char * name,State * state,const std::vector<std::unique_ptr<Expr>> & argv)30 Value* firmware_update(const char *name, State * state,
31                        const std::vector<std::unique_ptr<Expr>>& argv) {
32 	printf("%s: running %s.\n", __func__, name);
33 	if (argv.size() != 2) {
34 		ErrorAbort(state, kArgsParsingFailure, "syntax: %s bios.bin ec.bin", name);
35 		return nullptr;
36 	}
37 	std::vector<std::unique_ptr<Value>> args;
38 	if (!ReadValueArgs(state, argv, &args)) {
39 		ErrorAbort(state, kArgsParsingFailure, "%s: invalid arguments", name);
40 		return nullptr;
41 	}
42 	const Value *firmware = args[0].get();
43 	const Value *ec = args[1].get();
44 
45 	Value *retval = nullptr;
46 	int res = update_fw(firmware, ec, 0);
47 	if (res < 0) {
48 		ErrorAbort(state, kVendorFailure, "%s: firmware update error", name);
49 	} else {
50 		retval = StringValue(res ? "UPDATED" : "");
51 	}
52 
53 	printf("%s: [%s] done.\n", __func__,
54 		retval ? retval->data.c_str() : state->errmsg.c_str());
55 	return retval;
56 }
57 
Register_librecovery_updater_dragon()58 void Register_librecovery_updater_dragon() {
59 	RegisterFunction("dragon.firmware_update", firmware_update);
60 }
61