Update QCA956x GPIO OUT functions list
[oweals/u-boot_mod.git] / u-boot / net / httpd.c
1 /*
2  *      Copyright 1994, 1995, 2000 Neil Russell.
3  *      (See License)
4  *      Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <net.h>
10 #include <asm/byteorder.h>
11
12 #if defined(CONFIG_CMD_HTTPD)
13 #include "httpd.h"
14
15 #include "../httpd/uipopt.h"
16 #include "../httpd/uip.h"
17 #include "../httpd/uip_arp.h"
18
19 extern flash_info_t flash_info[];
20
21 static int arptimer = 0;
22
23 void HttpdHandler(void){
24         int i;
25
26         for(i = 0; i < UIP_CONNS; i++){
27                 uip_periodic(i);
28
29                 if(uip_len > 0){
30                         uip_arp_out();
31                         NetSendHttpd();
32                 }
33         }
34
35         if(++arptimer == 20){
36                 uip_arp_timer();
37                 arptimer = 0;
38         }
39 }
40
41 // start http daemon
42 void HttpdStart(void){
43         uip_init();
44         httpd_init();
45 }
46
47 int do_http_upgrade(const ulong size, const int upgrade_type){
48         char buf[96];   // erase 0xXXXXXXXX +0xXXXXXXXX; cp.b 0xXXXXXXXX 0xXXXXXXXX 0xXXXXXXXX (68 signs)
49         flash_info_t *info = &flash_info[0];
50         unsigned int backup_size = 0;
51
52         if(upgrade_type == WEBFAILSAFE_UPGRADE_TYPE_UBOOT){
53
54                 while(size > backup_size){
55                         backup_size += info->sector_size;
56                 }
57
58                 // Backup data from FLASH just before U-Boot image upgrade
59                 // if uploaded image size is not a multiple of FLASH erase sector size (by default 64 KB)
60                 if(size % info->sector_size != 0){
61                         printf("Backup: copying %d bytes of data from FLASH at address 0x%X to RAM at address 0x%X...\n",
62                                         backup_size - size,
63                                         CFG_FLASH_BASE + size,
64                                         CONFIG_LOADADDR + size);
65
66                         sprintf(buf,
67                                         "cp.b 0x%lX 0x%lX 0x%lX",
68                                         CFG_FLASH_BASE + size,
69                                         CONFIG_LOADADDR + size,
70                                         backup_size - size);
71
72                         if(!run_command(buf, 0)){
73                                 printf_err("couldn't backup FLASH data before U-Boot image upgrade!\n");
74                                 return(-1);
75                         }
76                 }
77
78                 printf("\n\n****************************\n*     U-BOOT UPGRADING     *\n* DO NOT POWER OFF DEVICE! *\n****************************\n\n");
79                 sprintf(buf,
80                                 "erase 0x%lX +0x%lX; cp.b 0x%lX 0x%lX 0x%lX",
81                                 CFG_FLASH_BASE,
82                                 backup_size,
83                                 CONFIG_LOADADDR,
84                                 CFG_FLASH_BASE,
85                                 backup_size);
86
87         } else if(upgrade_type == WEBFAILSAFE_UPGRADE_TYPE_FIRMWARE){
88
89                 printf("\n\n****************************\n*    FIRMWARE UPGRADING    *\n* DO NOT POWER OFF DEVICE! *\n****************************\n\n");
90                 sprintf(buf,
91                                 "erase 0x%lX +0x%lX; cp.b 0x%lX 0x%lX 0x%lX",
92                                 WEBFAILSAFE_UPLOAD_KERNEL_ADDRESS,
93                                 size,
94                                 CONFIG_LOADADDR,
95                                 WEBFAILSAFE_UPLOAD_KERNEL_ADDRESS,
96                                 size);
97
98         } else if(upgrade_type == WEBFAILSAFE_UPGRADE_TYPE_ART){
99
100                 // TODO: add option to change ART partition offset,
101                 // for those who want to use OFW on router with replaced/bigger FLASH
102                 printf("\n\n****************************\n*      ART  UPGRADING      *\n* DO NOT POWER OFF DEVICE! *\n****************************\n\n");
103 #if defined(WEBFAILSAFE_UPLOAD_ART_ADDRESS)
104                 sprintf(buf,
105                                 "erase 0x%lX +0x%lX; cp.b 0x%lX 0x%lX 0x%lX",
106                                 WEBFAILSAFE_UPLOAD_ART_ADDRESS,
107                                 WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES,
108                                 CONFIG_LOADADDR,
109                                 WEBFAILSAFE_UPLOAD_ART_ADDRESS,
110                                 WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES);
111 #else
112                 sprintf(buf,
113                                 "erase 0x%lX +0x%lX; cp.b 0x%lX 0x%lX 0x%lX",
114                                 CFG_FLASH_BASE + (info->size - WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES),
115                                 WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES,
116                                 CONFIG_LOADADDR,
117                                 CFG_FLASH_BASE + (info->size - WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES),
118                                 WEBFAILSAFE_UPLOAD_ART_SIZE_IN_BYTES);
119 #endif
120
121         } else {
122                 return(-1);
123         }
124
125         printf("Executing: %s\n\n", buf);
126         return(run_command(buf, 0));
127
128         return(-1);
129 }
130
131 // info about current progress of failsafe mode
132 int do_http_progress(const int state){
133         unsigned char i = 0;
134
135         /* toggle LED's here */
136         switch(state){
137                 case WEBFAILSAFE_PROGRESS_START:
138
139                         // blink LED fast 10 times
140                         for(i = 0; i < 10; ++i){
141                                 all_led_on();
142                                 milisecdelay(25);
143                                 all_led_off();
144                                 milisecdelay(25);
145                         }
146
147                         printf("HTTP server is ready!\n\n");
148                         break;
149
150                 case WEBFAILSAFE_PROGRESS_TIMEOUT:
151                         //printf("Waiting for request...\n");
152                         break;
153
154                 case WEBFAILSAFE_PROGRESS_UPLOAD_READY:
155                         printf("HTTP upload is done! Upgrading...\n");
156                         break;
157
158                 case WEBFAILSAFE_PROGRESS_UPGRADE_READY:
159                         printf("HTTP ugrade is done! Rebooting...\n\n");
160                         break;
161
162                 case WEBFAILSAFE_PROGRESS_UPGRADE_FAILED:
163                         printf_err("HTTP ugrade failed!\n\n");
164
165                         // blink LED fast for 4 sec
166                         for(i = 0; i < 80; ++i){
167                                 all_led_on();
168                                 milisecdelay(25);
169                                 all_led_off();
170                                 milisecdelay(25);
171                         }
172
173                         // wait 1 sec
174                         milisecdelay(1000);
175
176                         break;
177         }
178
179         return(0);
180 }
181 #endif /* CONFIG_CMD_HTTPD */