免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6245 | 回复: 2

[BootLoader] 用uip实现HTTP的 u-boot, 已经在我的WR703N上试验成功 [复制链接]

论坛徽章:
0
发表于 2013-01-17 19:21 |显示全部楼层
本帖最后由 qiushui_007 于 2013-01-17 19:22 编辑

参考这里

可惜的是挑浏览器, FF下成功, IE下不成功.

相关代码如下:
  1. void
  2. httpd_appcall(void)
  3. {
  4.         struct fs_file fsfile;
  5.         u8_t i;
  6.         switch(uip_conn->lport) {
  7.         case HTONS(80):
  8.                 hs = (struct httpd_state *)(uip_conn->appstate);
  9.                 if(uip_connected())
  10.                 {
  11.                         hs->state = HTTP_NONE;
  12.                         hs->count = 0;
  13.                         return;
  14.                 } else if(uip_poll())
  15.                 {
  16.                         if(hs->count++ >= 1000) {
  17.                                 uip_abort();
  18.                         }
  19.                         return;
  20.                 } else if(uip_newdata() && hs->state == HTTP_NONE)
  21.                 {
  22.                         if(uip_appdata[0] == ISO_G &&
  23.                                 uip_appdata[1] == ISO_E &&
  24.                                 uip_appdata[2] == ISO_T &&
  25.                                 uip_appdata[3] == ISO_space)
  26.                         {
  27.                                 hs->state = HTTP_FILE;
  28.                         }
  29.                         if(uip_appdata[0] == ISO_P &&
  30.                                 uip_appdata[1] == ISO_O &&
  31.                                 uip_appdata[2] == ISO_S &&
  32.                                 uip_appdata[3] == ISO_T &&
  33.                                 uip_appdata[4] == ISO_space)
  34.                         {
  35.                                 hs->state = HTTP_FIRMWARE;
  36.                         }
  37.                         if(hs->state == HTTP_NONE)
  38.                         {
  39.                                 uip_abort();
  40.                                 return;
  41.                         }
  42.                         if(hs->state == HTTP_FILE)
  43.                         {
  44.                                 for(i = 4; i < 40; ++i)
  45.                                 {
  46.                                         if(uip_appdata[i] == ISO_space ||
  47.                                                 uip_appdata[i] == ISO_cr ||
  48.                                                 uip_appdata[i] == ISO_nl)
  49.                                         {
  50.                                                 uip_appdata[i] = 0;
  51.                                                 break;
  52.                                         }
  53.                                 }

  54.                                 PRINT("request for file ");
  55.                                 PRINTLN(&uip_appdata[4]);
  56.                                 if(uip_appdata[4] == ISO_slash &&
  57.                                         uip_appdata[5] == 0)
  58.                                 {
  59.                                         fs_open(file_index_html.name, &fsfile);
  60.                                 } else {
  61.                                         if(!fs_open((const char *)&uip_appdata[4], &fsfile))
  62.                                         {
  63.                                                 PRINTLN("couldn't open file");
  64.                                                 fs_open(file_404_html.name, &fsfile);
  65.                                         }
  66.                                 }
  67.                                 hs->script = 0;
  68.                                 hs->state = HTTP_FILE;
  69.                                 hs->dataptr = fsfile.data;
  70.                                 hs->count = fsfile.len;
  71.                         }
  72.                         if(hs->state == HTTP_FIRMWARE)
  73.                         {
  74.                                 unsigned char *start = (unsigned char*)uip_appdata;
  75.                                 char *clen = strstr((char *)start, "Content-Length:");
  76.                                 int len = 0;
  77.                                 unsigned char *next, *end;
  78.                                 unsigned char *boundary_start;
  79.                                 int i;
  80.                                 uip_appdata[uip_len] = '\0';
  81.                                 if(clen)
  82.                                 {
  83.                                         clen += sizeof("Content-Length:");
  84.                                         next = (unsigned char *)strstr(clen, eol);
  85.                                         if(next)
  86.                                         {
  87.                                                 len = atoi(clen);
  88.                                                 next++;
  89.                                                 printf("expecting %d bytes\n", len);
  90.                                                 upload_data = httpd_upload_data = (unsigned char *)do_http_tmp_address();
  91.                                                 printf("received data will be stored at %p\n", upload_data);
  92.                                                 if(!upload_data)
  93.                                                 {
  94.                                                         printf("failed to allocate memory\n");
  95.                                                         uip_close();
  96.                                                         return;
  97.                                                 }
  98.                                         } else {
  99.                                                 uip_close();
  100.                                                 return;
  101.                                         }
  102.                                 }
  103.                                 if(len < 4 * 1024)
  104.                                 {
  105.                                         uip_close();
  106.                                         return;
  107.                                 }
  108.                                 boundary_start = (unsigned char *)strstr((char *)next, "---");
  109.                                 if(!boundary_start)
  110.                                 {
  111.                                         uip_close();
  112.                                         return;
  113.                                 }
  114.                                 end = (unsigned char *)strstr((char *)boundary_start, eol);
  115.                                 if(!end)
  116.                                 {
  117.                                         uip_close();
  118.                                         return;
  119.                                 }
  120.                                 boundary_len = end - boundary_start;
  121.                                 memcpy(boundary, boundary_start, boundary_len);
  122.                                 boundary[boundary_len] = 0;
  123.                                 next = (unsigned char *)strstr((char *)boundary_start, "name=\"firmware\";");
  124.                                 if(!next)
  125.                                 {
  126.                                         uip_close();
  127.                                         return;
  128.                                 }
  129.                                 next = (unsigned char *)strstr((char *)next, eol2);
  130.                                 if(!next)
  131.                                 {
  132.                                         printf("could not find start of data\n");
  133.                                         uip_close();
  134.                                         return;
  135.                                 }
  136.                                 next += 4;
  137.                                 hs->script = 0;
  138.                                 hs->state = HTTP_FIRMWARE;
  139.                                 hs->upload = uip_len - (next - start);
  140.                                 hs->upload_total = len - (int)(next - boundary_start);
  141.                                 hs->upload_total -= (strlen(boundary) + 6);
  142.                                 //printf("storing %d bytes at %p\n", (int)hs->upload, upload_data);
  143.                                 for(i = 0; i < hs->upload; i++)
  144.                                         upload_data[i] = next[i];
  145.                                 upload_data += (int)hs->upload;
  146.                                 printf("%d / %d\n", (int)hs->upload, hs->upload_total);
  147.                                 uip_slen = 0;
  148.                                 return;
  149.                         }
  150.                 }

  151.                 if(hs->state == HTTP_FIRMWARE)
  152.                 {
  153.                         if(uip_newdata())
  154.                         {
  155.                                 int i;
  156.                                 hs->count = 0;
  157.                                 uip_appdata[uip_len] = '\0';
  158.                                 hs->upload += uip_len;
  159.                                 //printf("storing %d bytes at %p\n", uip_len, upload_data);
  160.                                 printf("%d / %d\n", (int)hs->upload, hs->upload_total);
  161.                                 for(i = 0; i < uip_len; i++)
  162.                                         upload_data[i] = uip_appdata[i];
  163.                                 upload_data += uip_len;
  164.                                 uip_slen = 0;
  165.                                 if(hs->upload >= hs->upload_total)
  166.                                 {
  167.                                         upload_running = 1;
  168.                                         NetBootFileXferSize = hs->upload_total;
  169.                                         fs_open(file_flash_html.name, &fsfile);
  170.                                         hs->script = 0;
  171.                                         hs->state = HTTP_FILE;
  172.                                         hs->dataptr = fsfile.data;
  173.                                         hs->count = fsfile.len;
  174.                                 }
  175.                         }
  176.                 }
  177.                 if(hs->state == HTTP_FILE)
  178.                 {
  179.                         if(uip_acked())
  180.                         {
  181.                                 if(hs->count >= uip_conn->len)
  182.                                 {
  183.                                         hs->count -= uip_conn->len;
  184.                                         hs->dataptr += uip_conn->len;
  185.                                 } else {
  186.                                         hs->count = 0;
  187.                                 }
  188.                                 if(hs->count == 0)
  189.                                 {
  190.                                         if(upload_running)
  191.                                         {
  192.                                                 httpd_upload_complete = 1;
  193.                                         //        for(i = 0; i < hs->upload_total; i++)
  194.                                         //                printf("%c", httpd_upload_data[i]);
  195.                                         }
  196.                                         uip_close();
  197.                                 }
  198.                         }
  199.                         uip_send((unsigned char *)hs->dataptr, hs->count);
  200.                 }
  201.                 break;

  202.         default:
  203.                 uip_abort();
  204.                 break;
  205.         }
  206. }
复制代码

论坛徽章:
0
发表于 2013-01-17 19:55 |显示全部楼层
学习了                           

论坛徽章:
0
发表于 2013-01-18 15:18 |显示全部楼层
  1. //--- include/config.h, 生成的内容如下
  2. #define CONFIG_AR7240 1
  3. #define CONFIG_MACH_HORNET 1
  4. #define CONFIG_HORNET_1_1_WAR 1
  5. #define NEW_DDR_TAP_CAL 1
  6. #define FLASH_SIZE 4

  7. /* Automatically generated - do not edit */
  8. #include <configs/ap121.h>
  9. #define CONFIG_PID_WR70301        1
  10. #define GPIO_SYS_LED_BIT        27
  11. #define GPIO_SYS_LED_ON        0
  12. #define GPIO_RST_BUTTON_BIT        11

  13. ---- include/configs/ap121.h, 相关的配置数据都在此文件!!!!
  14. //#define FLASH_SIZE 4

  15. #if (FLASH_SIZE == 8)
  16. #define CFG_MAX_FLASH_BANKS     1            /* max number of memory banks */
  17. #define CFG_MAX_FLASH_SECT      128     /* max number of sectors on one chip */
  18. #define CFG_FLASH_SECTOR_SIZE   (64*1024)
  19. #define CFG_FLASH_SIZE          0x00800000 /* Total flash size */

  20. #elif (FLASH_SIZE == 4)
  21. #define CFG_MAX_FLASH_BANKS     1            /* max number of memory banks */
  22. #define CFG_MAX_FLASH_SECT      64      /* max number of sectors on one chip */
  23. #define CFG_FLASH_SECTOR_SIZE   (64*1024)
  24. #define CFG_FLASH_SIZE          0x00400000 /* Total flash size */

  25. #else
  26. /* For 2 MB flash */
  27. #        error "not support 2MB flash now"
  28. #define CFG_MAX_FLASH_BANKS     1            /* max number of memory banks */
  29. #define CFG_MAX_FLASH_SECT      32      /* max number of sectors on one chip */
  30. #define CFG_FLASH_SECTOR_SIZE   (64*1024)
  31. #define CFG_FLASH_SIZE          0x00200000 /* Total flash size */
  32. #endif

  33. #define CONFIG_IPADDR   192.168.1.1
  34. #define CONFIG_SERVERIP 192.168.1.2

  35. ----- net/uip-0.9/fsdata.c, httpd.c, fsdata.c,
  36. #升级时的提示
  37. Flashing...
  38. The system is now trying to flash. If there is a problem, the LEDs will start to blink.
  39. After a successful update the box will reboot

  40. static const char data_index_html[] =
  41. "HTTP/1.0 200 OK\n"
  42. "Server: uIP/0.9 (http://dunkels.com/adam/uip/)\n"
  43. "Content-type: text/html\n"
  44. "\n"
  45. "<html>\n"
  46. "\t<head>\n"
  47. "\t\t<title>\n"
  48. "\t\t\tFailsafe UI\n"
  49. "\t\t</title>\n"
  50. "\t</head>\n"
  51. "\t<body>\n"
  52. "\t\t<h1>Failsafe UI</h1>\n"
  53. "\t\t<form method=\"post\" enctype=\"multipart/form-data\">\n"
  54. "\t\t\t<input type=file name=firmware>\n"
  55. "\t\t\t<input type=submit>\n"
  56. "\t\t</form>\n"
  57. "\t</body>\n"
  58. "</html>\n";

  59. const struct fsdata_file file_index_html[] =
  60. {{file_404_html, "/index.html", data_index_html, sizeof(data_index_html)}};

  61. -- httpd.c
  62. void httpd_init(void)
  63. {
  64.         fs_init();
  65.         uip_listen(HTONS(80));
  66. }
  67. upload_data = httpd_upload_data = (unsigned char *)do_http_tmp_address();
  68. printf("received data will be stored at %p\n", upload_data);
  69. next = (unsigned char *)strstr((char *)boundary_start, "name=\"firmware\";");

  70. void httpd_appcall(void)
  71. {
  72.         //第一包
  73.         if(hs->state == HTTP_FIRMWARE) {
  74.                 next = (unsigned char *)strstr((char *)boundary_start, "name=\"firmware\";");
  75.                 if(!next)
  76.                 {
  77.                         uip_close();
  78.                         return;
  79.                 }
  80.                 next = (unsigned char *)strstr((char *)next, eol2);        //\r\n\r\n
  81.                 if(!next)
  82.                 {
  83.                         printf("could not find start of data\n");
  84.                         uip_close();
  85.                         return;
  86.                 }
  87.                 next += 4;        //此为首包的有效数据
  88.                 hs->script = 0;
  89.                 hs->state = HTTP_FIRMWARE;
  90.                 hs->upload = uip_len - (next - start);        //有效数据的长度
  91.                 hs->upload_total = len - (int)(next - boundary_start);
  92.                 hs->upload_total -= (strlen(boundary) + 6);
  93.                 //printf("storing %d bytes at %p\n", (int)hs->upload, upload_data);
  94.                 for(i = 0; i < hs->upload; i++)
  95.                         upload_data[i] = next[i];        //有效数据赋值
  96.                 upload_data += (int)hs->upload;
  97.                 printf("%d / %d\n", (int)hs->upload, hs->upload_total);
  98.                 uip_slen = 0;
  99.                 return;
  100.         }

  101.         //后续包
  102.         if(hs->state == HTTP_FIRMWARE) {
  103.                 if(uip_newdata()) {
  104.                         int i;
  105.                         hs->count = 0;
  106.                         uip_appdata[uip_len] = '\0';
  107.                         hs->upload += uip_len;
  108.                         //printf("storing %d bytes at %p\n", uip_len, upload_data);
  109.                         printf("%d / %d\n", (int)hs->upload, hs->upload_total);
  110.                         for(i = 0; i < uip_len; i++)
  111.                                 upload_data[i] = uip_appdata[i];
  112.                         upload_data += uip_len;
  113.                         uip_slen = 0;
  114.                         if(hs->upload >= hs->upload_total)
  115.                         {
  116.                                 upload_running = 1;
  117.                                 NetBootFileXferSize = hs->upload_total;
  118.                                 fs_open(file_flash_html.name, &fsfile);
  119.                                 hs->script = 0;
  120.                                 hs->state = HTTP_FILE;
  121.                                 hs->dataptr = fsfile.data;
  122.                                 hs->count = fsfile.len;
  123.                         }
  124.                 }
  125.         }
  126. }

  127. ---- net目录下
  128. -- httpd.c
  129. void HttpdStart (void)
  130. {
  131.         uip_init();
  132.         httpd_init();
  133. }

  134. unsigned long do_http_tmp_address(void)
  135. {
  136.         char *s = getenv ("ram_addr");
  137.         if (s) {
  138.                 ulong tmp = simple_strtoul (s, NULL, 16);
  139.                 return tmp;
  140.         }
  141.         return 0 /*0x80a00000*/;
  142. }

  143. int do_http_progress(const int state)
  144. {
  145.         /* toggle LED's here */
  146.         switch(state) {
  147.                 case HTTP_PROGRESS_START:        ar7240_all_led_on(); break;
  148.                 case HTTP_PROGRESS_UPLOAD_READY:
  149.                         ar7240_all_led_off();
  150.                         udelay(1000 * 500);
  151.                         ar7240_all_led_on();
  152.                         break;
  153.                 case HTTP_PROGRESS_UGRADE_FAILED:
  154.                         puts("http ugrade failed\n");
  155.                         break;
  156.         }
  157.         return 0;
  158. }

  159. int do_http_upgrade(const unsigned char *data, const ulong size)
  160. {
  161.         char buf[128];

  162.         if(getenv ("ram_addr") == NULL)                 return -1;
  163.         if(getenv ("kernel_addr") == NULL)         return -1;
  164.         /* write the image to the flash, 执行命令 刷FW
  165.         erase 0x9f020000 +0x7c0000
  166.         cp.b 0x80000000 0x9f020000 0x7c0000
  167.         */
  168.         sprintf(buf, "era ${kernel_addr} +0x%lx; cp.b ${ram_addr} ${kernel_addr} 0x%lx", size, size);        //
  169.         return run_command(buf, 0);        //代码中调用cmd
  170. }

  171. -- net.c
  172. int NetLoopHttpd(void)
  173. {
  174.         HttpdStart();
  175.         do_http_progress(HTTP_PROGRESS_START);

  176.         https_running = 1;
  177.         for (;;) {
  178.                 unsigned long long t1;
  179.                 WATCHDOG_RESET();
  180.                 if(eth_rx() > 0) {
  181.                         HttpdHandler();
  182.                 } else {
  183.                         t1 = get_ticks();
  184.                         if(t1 - tout > 1000) {
  185.                                 do_http_progress(HTTP_PROGRESS_TIMEOUT);
  186.                                 tout = t1;
  187.                         }
  188.                 }

  189.                 do_http_progress(HTTP_PROGRESS_UPLOAD_READY);
  190.                 //http升级
  191.                 if(do_http_upgrade(&httpd_upload_data[0], NetBootFileXferSize) >= 0) {
  192.                         do_http_progress(HTTP_PROGRESS_UGRADE_READY);
  193.                         udelay(1000 * 10);
  194.                         ar7240_all_led_off();
  195.                         do_reset (0,0,0,0);
  196.                         return 0;
  197.                 }
  198.                 break;
  199.         }

  200. }

  201. ---- board/ar7240/ap121 目录下, ap121.c, flash.c
  202. #define        MY_WRITE(y, z)        ((*((volatile u32*)(y))) = z)
  203. #define        MY_READ(y)                        (*((volatile u32*)(y)))
  204. #define SETBITVAL(val, pos, bit)                                                                                                                        \
  205.   do {                                                                                                                                                                                                                                 \
  206.                 ulong bitval = (bit) ? 0x1 : 0x0;                                                                                                        \
  207.                 (val) = ((val) & ~(0x1 << (pos))) | ( (bitval) << (pos)); \
  208.   } while (0)

  209. void ar7240_all_led_on(void)
  210. {
  211.         ulong gpio;
  212.         int index;

  213.         gpio = MY_READ(0xb8040008);
  214. #ifdef CONFIG_PID_WR70301
  215.         SETBITVAL(gpio, GPIO_SYS_LED_BIT, GPIO_SYS_LED_ON);                //LED ON
  216.         SETBITVAL(gpio, GPIO_SYS_LED_BIT, !GPIO_SYS_LED_ON);        //LED OFF
  217. #endif
  218.         MY_WRITE(0xb8040008, gpio);
  219. }

  220. int ar7240_is_rst_button_pushed(void)
  221. {
  222.         if(ar7240_reg_rd(AR7240_GPIO_IN) & (1 << GPIO_RST_BUTTON_BIT))        return 1;
  223.         else                return 0;
  224. }

  225. //sets up flash_info and returns size of FLASH (bytes)
  226. unsigned long flash_get_geom (flash_info_t *flash_info)
  227. {
  228.     int i;

  229.     //#define FLASH_M25P64    0x00F2, \include\flash.h
  230.     flash_info->flash_id  = FLASH_M25P64;
  231.     //#define CFG_FLASH_SIZE                ((uint)(8 * 1024 * 1024))        /* max 8Mbyte        */, \board\fads\fads.h,
  232.     flash_info->size = CFG_FLASH_SIZE; /* bytes */
  233.     flash_info->sector_count = flash_info->size/CFG_FLASH_SECTOR_SIZE;

  234.     for (i = 0; i < flash_info->sector_count; i++) {
  235.         flash_info->start[i] = CFG_FLASH_BASE + (i * CFG_FLASH_SECTOR_SIZE);
  236.         flash_info->protect[i] = 0;
  237.     }

  238.     printf ("flash size %d, sector count = %d\n", flash_info->size, flash_info->sector_count);
  239.     return (flash_info->size);
  240. }

  241. ---- board/ar7240/common目录下,
  242. -- ar7240_flash.c: 通过SPI 读写Flash
  243. unsigned long flash_init(void)
  244. {
  245.         read_id();
  246.         return (flash_get_geom(&flash_info[0]));
  247. }

  248. int flash_erase(flash_info_t *info, int s_first, int s_last)
  249. {
  250.         int i, sector_size = info->size / info->sector_count;

  251.         for (i = s_first; i <= s_last; i++) {
  252.                 ar7240_spi_sector_erase(i * sector_size);
  253.         }
  254.         ar7240_spi_done();

  255.         return 0;
  256. }

  257. -- main.c
  258. void main_loop (void)
  259. {

  260. # ifdef CONFIG_CMD_HTTPD
  261.                 if (ar7240_is_rst_button_pushed()) {
  262.                         printf("You push the button,starting httpd to update firmware ...\n");
  263.                         NetLoopHttpd();
  264.                 }
  265. # endif
  266. }

  267. int run_command (const char *cmd, int flag)
  268. {
  269. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP