-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader_ar6mx_updater.c
77 lines (63 loc) · 2.35 KB
/
bootloader_ar6mx_updater.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "edify/expr.h"
#include "bootloader_ar6mx.h"
Value* WriteBootloaderFn(const char* name, State* state, int argc, Expr* argv[]) {
char *srcPath = NULL, *dstPath = NULL;
unsigned long seek = 0, skip = 0;
const char *btLdrFrRoNode = "/sys/block/mmcblk3boot0/force_ro";
const char *btLdrEnableBoot = "/sys/block/mmcblk3/device/boot_config";
int result = -1;
Value* srcPathVal;
Value* dstPathVal;
Value* seekVal;
Value* skipVal;
// sanity check inputs and extract
if (argc != 4) {
return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
}
if (ReadValueArgs(state, argv, 4,
&srcPathVal, &dstPathVal, &seekVal, &skipVal) < 0) {
return NULL;
}
// extract data from Value structures
srcPath = malloc(sizeof(char) * strlen(srcPathVal->data));
strcpy(srcPath, srcPathVal->data);
dstPath = malloc(sizeof(char) * strlen(dstPathVal->data));
strcpy(dstPath, dstPathVal->data);
seek = atol(seekVal->data);
skip = atol(skipVal->data);
// write the new bootloader, unlock to write, write the file to emmc, and relock
result = write_sysfs(0, btLdrFrRoNode);
if (result < 0) {
return ErrorAbort(state, "Unlocking MMC boot partition failed");
}
result = update_bootloader(srcPath, dstPath, seek, skip);
if (result < 0) {
return ErrorAbort(state, "Writing the bootloader from %s to %s with seek=%d and skip=%d failed, your unit may now be bricked result=%d",
srcPath, dstPath, seek, skip, result);
}
result = write_sysfs(1, btLdrFrRoNode);
if (result < 0) {
return ErrorAbort(state, "Relocking MMC boot partition failed");
}
// make sure bootloader is bootable
result = write_sysfs(8, btLdrEnableBoot);
if (result < 0) {
return ErrorAbort(state, "Relocking MMC boot partition failed");
}
// be a good citizen, free your resources
FreeValue(srcPathVal);
FreeValue(dstPathVal);
FreeValue(seekVal);
FreeValue(skipVal);
// return a good result here
return StringValue(strdup(result == 0 ? "t" : ""));
}
void Register_librecovery_updater_ar6mx() {
fprintf(stderr, "installing PDi(R) ar6mx updater extensions\n");
RegisterFunction("ar6mx.write_bootloader", WriteBootloaderFn);
}