OpenWrt – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * TP-Link recovery flag set and unset code for ramips target
3 *
4 * Copyright (C) 2018 David Bauer <mail@david-bauer.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License v2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15  
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <unistd.h>
20 #include <sys/mman.h>
21 #include <errno.h>
22 #include <stdint.h>
23  
24 #include <mtd/mtd-user.h>
25 #include <sys/ioctl.h>
26  
27 #include "mtd.h"
28  
29  
30 #define TPL_RECOVER_MAGIC 0x89abcdef
31 #define TPL_NO_RECOVER_MAGIC 0x00000000
32  
33  
34 struct uboot_args {
35 uint32_t magic;
36 };
37  
38 int mtd_tpl_recoverflag_write(const char *mtd, const bool recovery_active)
39 {
40 struct erase_info_user erase_info;
41 struct uboot_args *args;
42 uint32_t magic;
43 int ret = 0;
44 int fd;
45  
46 args = malloc(erasesize);
47 if (!args) {
48 fprintf(stderr, "Could not allocate memory!\n");
49 return -1;
50 }
51  
52 fd = mtd_check_open(mtd);
53 if (fd < 0) {
54 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
55 ret = -1;
56 goto out;
57 }
58  
59 /* read first block (containing the magic) */
60 pread(fd, args, erasesize, 0);
61  
62 /* set magic to desired value */
63 magic = TPL_RECOVER_MAGIC;
64 if (!recovery_active)
65 magic = TPL_NO_RECOVER_MAGIC;
66  
67 /* no need to write when magic is already set correctly */
68 if (magic == args->magic)
69 goto out;
70  
71 /* erase first block (containing the magic) */
72 erase_info.start = 0;
73 erase_info.length = erasesize;
74  
75 ret = ioctl(fd, MEMERASE, &erase_info);
76 if (ret < 0) {
77 fprintf(stderr, "failed to erase block: %i\n", ret);
78 goto out;
79 }
80  
81 /* write magic to flash */
82 args->magic = magic;
83  
84 ret = pwrite(fd, args, erasesize, 0);
85 if (ret < 0)
86 fprintf(stderr, "failed to write: %i\n", ret);
87  
88 sync();
89 out:
90 free(args);
91 close(fd);
92  
93 return ret;
94 }