summaryrefslogtreecommitdiff
path: root/release/src/router/shared/backup_restore.c
blob: 75c65a1277105fa42922d9d7cd4cd3917fe08cc3 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

/*
 *********************************************************
 *   Copyright 2003, CyberTAN  Inc.  All Rights Reserved *
 *********************************************************

 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
 the contents of this file may not be disclosed to third parties,
 copied or duplicated in any form without the prior written
 permission of CyberTAN Inc.

 This software should be used as a reference only, and it not
 intended for production use!


 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE.  CYBERTAN
 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#include <broadcom.h>
#include <cyutils.h>
#include <code_pattern.h>

extern struct code_header *init_code_header(void);

void
do_backup(char *path, webs_t stream) 
{
	FILE *fp_r, *fp_w;
	char buf[NVRAM_SPACE];
	struct code_header *pattern;

	memset(&buf, 0, sizeof(buf));
	
	pattern = malloc(sizeof(struct code_header));
        memset(pattern, 0, sizeof(struct code_header));

	pattern = (struct code_header *)init_code_header();

	/* Write code pattern */ 

        /* Open NVRAM file */
        if (!(fp_r = fopen("/dev/mtd/3", "r"))) {
                perror("/dev/mtd/3");
                return;
        }
        fclose(fp_r);
        if (!(fp_r = fopen("/dev/mtd/4", "r"))) {
                perror("/dev/mtd/4");
                return;
        }

        /* Read NVRAM data */
        fseek(fp_r, -NVRAM_SPACE, SEEK_END);
       	fread(buf, NVRAM_SPACE, 1, fp_r);
        fclose(fp_r);
	
	encode(buf, sizeof(buf));
	
	if(!(fp_w = fopen("/tmp/config.bin", "w"))){
		perror("/tmp/config.bin");
		if(pattern)	free(pattern);
		return ;
	}

	/* Write code pattern */
	/* Due to the 4702/4712/4712L/4704 use different boot, restoring different backup files into different HW version, will cause Router crash */
	if(check_hw_type() == BCM5352E_CHIP)
		pattern->hw_ver = 4;
	else if(check_hw_type() == BCM4704_BCM5325F_CHIP)
		pattern->hw_ver = 3;
	else if(check_hw_type() == BCM5325E_CHIP)
		pattern->hw_ver = 2;
	else if(check_hw_type() == BCM4712_CHIP)
		pattern->hw_ver = 1;
	else
		pattern->hw_ver = 0;
		
	fwrite(pattern, 1, sizeof(struct code_header), fp_w);
	/*Write NVRAM data */
	fwrite(buf, 1, sizeof(buf), fp_w);
	fclose(fp_w);

        do_file("/tmp/config.bin", stream);

	if(pattern)	free(pattern);
}

int
ej_get_backup_name(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret;
	char buf[80];
	
	if(check_hw_type() == BCM4702_CHIP)
		snprintf(buf, sizeof(buf), "%sV1_%s.cfg", MODEL_NAME, CYBERTAN_VERSION);
	else if((check_hw_type() == BCM4712_CHIP) || (check_hw_type() == BCM5325E_CHIP))
		snprintf(buf, sizeof(buf), "%sV2_%s.cfg", MODEL_NAME, CYBERTAN_VERSION);
	else if(check_hw_type() == BCM4704_BCM5325F_CHIP)
		snprintf(buf, sizeof(buf), "%sV5_%s.cfg", MODEL_NAME, CYBERTAN_VERSION);
	else if(check_hw_type() == BCM5352E_CHIP)
		snprintf(buf, sizeof(buf), "%s%s_%s.cfg", MODEL_NAME, MODEL_VERSION, CYBERTAN_VERSION);
	else
		snprintf(buf, sizeof(buf), "%s_%s.cfg", MODEL_NAME, CYBERTAN_VERSION);

	ret = websWrite(wp, "%s", buf);

	return ret;
}	

int
ej_view_config(int eid, webs_t wp, int argc, char_t **argv)
{
	int ret = 0;
        FILE *fp;
        char line[10240];
	
	if ((fp = popen("nvram show | sort", "r")) != NULL) {
		while( fgets(line, sizeof(line), fp) != NULL ) {
			line[strlen(line)-1] = '\0';
			if(!strcmp(line,""))	continue;

			ret += websWrite(wp, "%s<br>\n", line);
		}
		pclose(fp);
	}

	return ret;
}