lusa 5 vuotta sitten
vanhempi
sitoutus
237bea344e

+ 2 - 0
app/Makefile

@@ -13,11 +13,13 @@ all	:
 	make -C ./goahead-3.6.5
 	make -C ./test_app
 	make -C ./ipmitool-1.8.18
+	make -C ./UpdateFirmware
 clean	: 
 	make -C ./goahead-3.6.5 clean
 	make -C ./bmc clean
 	make -C ./test_app clean
 	make -C ./ipmitool-1.8.18 clean
+	make -C ./UpdateFirmware clean
 
 	make -C ./driver/Platform clean
 	make -C ./driver/GPIO clean

+ 49 - 0
app/UpdateFirmware/Makefile

@@ -0,0 +1,49 @@
+EXEC		= updateFW_app
+
+CFLAGS		:= -Os -mcpu=cortex-m3 -mthumb
+LDFLAGS		:= -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/A2F/root/usr/lib
+#BUILD_TIME 	= $(shell date +"%Y-%m-%d %H:%M:%S")
+
+
+
+CC	= $(CROSS_COMPILE_APPS)gcc
+
+#source
+SRC	= main.c
+
+# hal_api
+SRC += $(wildcard ../hal_api/*.c) 
+
+#incldue
+CFLAGS	+= -I$(INSTALL_ROOT)/A2F/root/usr/include
+CFLAGS	+= -I ./
+CFLAGS	+= -I ../common_include
+CFLAGS	+= -I ../driver
+CFLAGS	+= -I ../hal_api
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc/Legacy
+
+
+#library
+
+#sub-directory
+#SUBDIR 	= ./sensor
+
+
+
+
+
+
+$(EXEC): $(SRC)
+	#echo $(BUILD_TIME)
+	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS) -lpthread
+
+
+#object
+OBJS	= $(SRC:%.c=%.o)
+
+clean:
+	rm -f $(EXEC) $(OBJS)
+	rm -f *.gdb 
+

+ 90 - 0
app/UpdateFirmware/main.c

@@ -0,0 +1,90 @@
+
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <linux/types.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <netdb.h>        /* getaddrinfo(3) et al.                       */
+#include <netinet/in.h>   /* sockaddr_in & sockaddr_in6 definition.      */
+#include  <net/if.h>
+#include <sys/prctl.h>
+#include "driver.h"
+#include "hal_interface_api.h"
+#include "main.h"
+
+int main()
+{
+	int i;
+	uint8_t buf[4*1024] = {0};	//4KB, one sector
+	FILE *fp;
+	size_t len, totalSize = 1, writeSize = 0;
+	FILE *fprogress;
+	uint8_t progressStr[10] = {0};
+	uint8_t rate = 0;
+
+	fp = fopen("/var/www/goahead/tmp/tmp.uImage", "rb");
+	if(NULL == fp)
+	{
+		printf("Open image file fail!\n");
+		return -1;
+	}
+
+	//get file total size
+	fseek(fp, 0, SEEK_END);
+	totalSize = ftell(fp);
+	fseek(fp, 0, SEEK_SET);
+	printf("Image size: %d\n", totalSize);
+
+	do{
+		len = fread(buf, 1, 4*1024, fp);
+		if(len <= 0)
+		{
+			close(fp);
+			printf("Read file fail!\n");
+			break;
+		}
+	
+		writeSize += len;
+		rate = (writeSize*100)/totalSize;
+		//printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);			
+
+		//用来保存更新的进度
+		fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
+		if(NULL == fp)
+		{
+			printf("Create UpdateProgress.log file fail!\n");
+			return -1;
+		}
+		sprintf(progressStr, "%d%%", rate);
+		fputs(progressStr, fprogress);
+		fclose(fprogress);
+
+		//TODO: update Flash
+		usleep(10000);
+		
+	}while(writeSize < totalSize);
+
+	printf("Update Flash finish.\n");
+
+	//Verify
+	printf("Verigy successful.\n");
+	//用来保存更新的进度
+	fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
+	if(NULL == fp)
+	{
+		printf("Create UpdateProgress.log file fail!\n");
+		return -1;
+	}
+	sprintf(progressStr, "Verify OK");
+	fputs(progressStr, fprogress);
+	fclose(fprogress);
+
+	fclose(fp);
+	return 0;
+}

+ 0 - 0
app/UpdateFirmware/main.h


BIN
app/UpdateFirmware/updateFW_app


+ 0 - 2
app/goahead-3.6.5/src/action.c

@@ -53,7 +53,6 @@ static bool actionHandler(Webs *wp)
         fn = (WebsAction) sp->content.value.symbol;
         assert(fn);
         if (fn) {
-            printf("---> actionName: %s\n", actionName);
 #if ME_GOAHEAD_LEGACY
             (*((WebsProc) fn))((void*) wp, actionName, wp->query);
 #else
@@ -61,7 +60,6 @@ static bool actionHandler(Webs *wp)
 #endif
         }
     }
-    printf("---> actionHandler return 1\n");
     return 1;
 }
 

+ 4 - 6
app/goahead-3.6.5/src/http.c

@@ -788,7 +788,6 @@ static void readEvent(Webs *wp)
         return;
     }
     websNoteRequestActivity(wp);
-
     rxbuf = &wp->rxbuf; //存储的是请求包中的所有数据 18432 //缓冲区的数据结构
 //    printf("%d>>>>>>>>0000>>>>%d>>>\n",rxbuf->buflen, bufRoom(rxbuf));
     if (bufRoom(rxbuf) < (ME_GOAHEAD_LIMIT_BUFFER+1)) { //缓冲区不够了增加缓冲区的大小
@@ -823,6 +822,7 @@ static void readEvent(Webs *wp)
         return;//通过websPump处理完请求,需要关闭连接,return返回readEvent.数据结构依然保留。
     } else if (nbytes < 0 && socketEof(wp->sid)) {
         /* EOF or error. Allow running requests to continue. */
+        // printf("%d>>>>>>>>>state>>>>>>>>>>>\n", wp->state);
         //state 值是1
         if (wp->state < WEBS_READY) {//ready 2
             if (wp->state > WEBS_BEGIN) {//begin 0
@@ -846,7 +846,6 @@ PUBLIC void websPump(Webs *wp)
     bool    canProceed;
 
     for (canProceed = 1; canProceed; ) {
-        printf("wp->state = %d\n",wp->state);
         switch (wp->state) {
         case WEBS_BEGIN:
             canProceed = parseIncoming(wp);
@@ -856,6 +855,7 @@ PUBLIC void websPump(Webs *wp)
             break;
         case WEBS_READY:
             if (!websRunRequest(wp)) {
+                // printf("%d<<<<<<<<<<<<>>>>>>>>>*******\n", WEBS_READY);
                 /* Reroute if the handler re-wrote the request */
                 websRouteRequest(wp);
                 wp->state = WEBS_READY;
@@ -1120,6 +1120,7 @@ static void parseHeaders(Webs *wp)
         } else if (strcmp(key, "content-type") == 0) {
             wfree(wp->contentType);
             wp->contentType = sclone(value);
+//            printf("%s>>>>>>>>>>>>>>>wp->contentType>>>>>\n", wp->contentType);
             if (strstr(value, "application/x-www-form-urlencoded")) {
                 wp->flags |= WEBS_FORM;
             } else if (strstr(value, "application/json")) {
@@ -1329,6 +1330,7 @@ static bool filterChunkData(Webs *wp)
             nbytes = min(bufRoom(&wp->input), len);
 //            printf("---> bufPutBlk2 len = %d\n", len);
             if (len > 0 && (nbytes = bufPutBlk(&wp->input, rxbuf->servp, nbytes)) == 0) {
+                printf("<<<<<<<<<<<<<<>>42222>>>>>>>>>>>>\n" );
                 websError(wp, HTTP_CODE_REQUEST_TOO_LARGE | WEBS_CLOSE, "Too big");
                 return 1;
             }
@@ -1666,16 +1668,13 @@ PUBLIC void websRedirect(Webs *wp, char *uri)
         port = secure ? defaultSslPort : defaultHttpPort;
     }
     if (strstr(uri, "https:///")) {
-        printf("---> log1\n");
         /* Short-hand for redirect to https */
         uri = location = makeUri(scheme, hostbuf, port, &uri[8]);
 
     } else if (strstr(uri, "http:///")) {
-        printf("---> log2\n");
         uri = location = makeUri(scheme, hostbuf, port, &uri[7]);
 
     } else if (!fullyQualified) {
-        printf("---> log3\n");
         uri = location = makeUri(scheme, hostbuf, port, uri);
     }
     message = sfmt("<html><head></head><body>\r\n\
@@ -1683,7 +1682,6 @@ PUBLIC void websRedirect(Webs *wp, char *uri)
         Please update your documents to reflect the new location.\r\n\
         </body></html>\r\n", uri);
     len = slen(message);
-    printf("---> message: %s\n", message);
     websSetStatus(wp, HTTP_CODE_MOVED_TEMPORARILY);
     websWriteHeaders(wp, len + 2, uri);
     websWriteEndHeaders(wp);

+ 1 - 1
app/goahead-3.6.5/src/upload.c

@@ -304,7 +304,7 @@ static int writeToFile(Webs *wp, char *data, ssize len)
 
     file = wp->currentFile;
     //printf("===file->size %d >>>len %d >> ME_GOAHEAD_LIMIT_UPLOAD %d ====\n", file->size, len, ME_GOAHEAD_LIMIT_UPLOAD);
-    printf("- %d\n", file->size);
+    //printf("- %d\n", file->size);
     if ((file->size + len) > ME_GOAHEAD_LIMIT_UPLOAD) {
         websError(wp, HTTP_CODE_REQUEST_TOO_LARGE, "Uploaded file exceeds maximum %d", (int) ME_GOAHEAD_LIMIT_UPLOAD);
         return -1;

+ 73 - 22
app/goahead-3.6.5/src/web_interface/src/fw_update.c

@@ -9,6 +9,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <string.h>
 
 static uint8_t progress = 0;
 /* 进入固件更新模式
@@ -17,20 +18,20 @@ static uint8_t progress = 0;
 void prepareDevice(Webs *wp)
 {
     printf("Update Firmware: Prepare Device...\n");
-    //TODO: kill all other process
+    uint8_t cmd[50] = {0};
     progress = 0;
-    sleep(10);
+    //kill bmc_app
+    sprintf(cmd, "killall -9 bmc_app"); 
+    system(cmd);
     char *pStr;
     cJSON * root =  cJSON_CreateObject();
     cJSON * data =  cJSON_CreateObject();
     cJSON_AddItemToObject(root, "data", data);//根节点下添加
     cJSON_AddStringToObject(root, "msg", "");
     cJSON_AddNumberToObject(root, "code", 200);
-    
-   
     pStr = cJSON_PrintUnformatted(root);
-
     printf("---> cJSON Str:\n%s\n", pStr);
+   
     websSetStatus(wp, 200);
     websWriteHeaders(wp, -1, 0);
     websWriteEndHeaders(wp); 
@@ -47,7 +48,8 @@ void prepareDevice(Webs *wp)
     printf("Update Firmware: Prepare Device Ok.\n");
 }
 
-/* 接收上传的固件
+/* 
+    接收上传的固件
 */
 
 void uploadFirmware(Webs *wp)
@@ -98,16 +100,35 @@ void updateFlash(Webs *wp)
 {
     printf("Update Firmware: Update Flash...\n");
     //TODO: 
-    
+    uint8_t cmd[50] = {0};
+    progress = 0;
 
-    sleep(10);
+    //Call updateFW_app
+    sprintf(cmd, "/usr/bin/updateFW_app &"); 
+    system(cmd);
+    char *pStr;
+    cJSON * root =  cJSON_CreateObject();
+    cJSON * data =  cJSON_CreateObject();
+    cJSON_AddItemToObject(root, "data", data);//根节点下添加
+    cJSON_AddStringToObject(root, "msg", "");
+    cJSON_AddNumberToObject(root, "code", 200);
+    pStr = cJSON_PrintUnformatted(root);
+    printf("---> cJSON Str:\n%s\n", pStr);
+   
     websSetStatus(wp, 200);
     websWriteHeaders(wp, -1, 0);
-    //websWriteHeader(wp, "updateFlash");
-    websWriteEndHeaders(wp);
-    //websWrite(wp, "%s", pStr);
-    //websFlush(wp, 0);
+    websWriteEndHeaders(wp); 
+    websWrite(wp,"%s", pStr);
+    websFlush(wp, 0);
+    // websDone(wp);
+
+    if(pStr)
+        wfree(pStr);
+    if(root)
+        cJSON_Delete(root);
+    
     websDone(wp);
+    printf("Update Firmware: Prepare Device Ok.\n");
 }
 
 
@@ -117,16 +138,32 @@ void updateFlash(Webs *wp)
 */
 void getUpdateProgress(Webs *wp)
 {
-    uint8_t progressStr[5] = {0};
-    //TODO: 
-    if(progress < 100)
-        progress++;
-    sprintf(progressStr, "%d%%", progress);
+    uint8_t progressStr[10] = {0};
+    FILE *fp;
+
+    //TODO:
+    fp = fopen("/var/www/goahead/tmp/UpdateProgress.log", "r");
+    if(NULL == fp)
+    {
+        printf("Open UpdateProgress.log failed!\n");
+        websError(wp, 200, "Open UpdateProgress.log failed!");
+        return;
+    }
+
+    if(NULL == fgets(progressStr, 10, fp))
+    {
+        printf("Read UpdateProgress.log failed!\n");
+        websError(wp, 200, "Read UpdateProgress.log failed!");
+        fclose(fp);
+        return;
+    }
+    
     printf("Update Firmware: %s\n", progressStr);
 
+
     char *pStr;
     cJSON * root =  cJSON_CreateObject();
-    cJSON_AddStringToObject(root, "msg", progressStr);
+    cJSON_AddStringToObject(root, "progress", progressStr);
     cJSON_AddNumberToObject(root, "code", 200);
     
 
@@ -146,13 +183,17 @@ void getUpdateProgress(Webs *wp)
         cJSON_Delete(root);
 }
 
+
 void resetBmc(Webs *wp)
 {
-    printf("Update Firmware: Reset BMC...\n");
-    //TODO: 
-    if(progress < 100)
-        progress++;
+#define DEV_NAME "/dev/platform"
+#define IOCTL_MAGIC 'x' 
+#define MAJOR_PLATFORM  100
+#define GENERATE_CMD(__MAGIC__, __MAJOR__, __CMD__) \
+    ((0x3<<30) | (__MAJOR__<<16) | (__MAGIC__<<8) | __CMD__)
+#define RESET_MCU                       GENERATE_CMD(IOCTL_MAGIC, MAJOR_PLATFORM, 2)
 
+    printf("Update Firmware: Reset BMC...\n");
     websSetStatus(wp, 200);
     websWriteHeaders(wp, -1, 0);
     //websWriteHeader(wp, "resetBmc");
@@ -160,6 +201,16 @@ void resetBmc(Webs *wp)
     //websWrite(wp, "%s", pStr);
     //websFlush(wp, 0);
     websDone(wp);
+
+    usleep(1000);  //wait previous code
+    int fd = open(DEV_NAME, O_RDWR);
+    if(fd == -1)
+    {
+        printf("Open %s failed!\n", DEV_NAME);
+    }
+
+    ioctl(fd, RESET_MCU, NULL);    
+    close(fd);
 }
 
 

+ 2 - 2
gd32450i-eval.busybox

@@ -1,7 +1,7 @@
 #
 # Automatically generated make config: don't edit
 # Busybox version: 1.17.0
-# Mon Mar 30 00:00:02 2020
+# Mon Apr 13 23:55:43 2020
 #
 CONFIG_HAVE_DOT_CONFIG=y
 
@@ -842,7 +842,7 @@ CONFIG_FEATURE_MIME_CHARSET=""
 CONFIG_FREE=y
 # CONFIG_FUSER is not set
 CONFIG_KILL=y
-# CONFIG_KILLALL is not set
+CONFIG_KILLALL=y
 # CONFIG_KILLALL5 is not set
 # CONFIG_NMETER is not set
 # CONFIG_PGREP is not set

+ 2 - 0
gd32450i-eval.initramfs

@@ -62,6 +62,7 @@ slink /bin/date busybox 777 0 0
 slink /bin/echo busybox 777 0 0
 slink /bin/hush busybox 777 0 0
 slink /bin/kill busybox 777 0 0
+slink /bin/killall busybox 777 0 0
 #slink /bin/ln busybox 777 0 0
 slink /bin/login /bin/busybox 777 0 0
 slink /bin/ls busybox 777 0 0
@@ -127,6 +128,7 @@ file /usr/bin/test_app ${INSTALL_ROOT}/projects/${SAMPLE}/app/test_app/test_app
 file /usr/bin/bmc_app ${INSTALL_ROOT}/projects/${SAMPLE}/app/bmc/bmc_app 755 0 0
 file /usr/bin/ipmitool ${INSTALL_ROOT}/projects/${SAMPLE}/app/ipmitool-1.8.18/ipmitool 755 0 0
 file /usr/bin/goahead-3.6.5 ${INSTALL_ROOT}/projects/${SAMPLE}/app/goahead-3.6.5/build/linux-arm-static/bin/goahead 755 0 0
+file /usr/bin/updateFW_app ${INSTALL_ROOT}/projects/${SAMPLE}/app/UpdateFirmware/updateFW_app 755 0 0
 
 dir /etc/goahead 755 0 0 
 dir /var/www 755 0 0