9d2581ac68be7a0ac6f0136075b47a9b4437f616
[platform/upstream/libwebsockets.git] / scripts / esp32.mk
1 #
2 # LWS-style images are composed like this
3 #
4 # [ OTA or Factory standard xpressif image ]
5 # [ 32-b LE len ] [ ROMFS ]
6 # [ 32-b LE len ] [ Image information JSON ]
7 #
8
9 SHELL=/bin/bash
10
11 jbi=$(COMPONENT_PATH)/../build/json-buildinfo
12
13 FAC=$(CONFIG_LWS_IS_FACTORY_APPLICATION)
14 ifeq ($(FAC),)
15         FAC=0
16 endif
17 export FAC
18
19 .PHONY: romfs.img
20 pack.img:
21         DIRNAME=$$(basename $$(pwd) | tr -d '\n') ;\
22         cp $(COMPONENT_PATH)/../build/$(PROJECT_NAME).bin $(COMPONENT_PATH)/../build/$$DIRNAME.bin ; \
23         genromfs -f $(COMPONENT_PATH)/../build/romfs.img -d $(COMPONENT_PATH)/../romfs-files ; \
24         RLEN=$$(stat -c %s $(COMPONENT_PATH)/../build/romfs.img) ;\
25         LEN=$$(stat -c %s $(COMPONENT_PATH)/../build/$$DIRNAME.bin) ;\
26         printf "             Original length: 0x%06x (%8d)\n" $$LEN $$LEN ; \
27         printf %02x $$(( $$RLEN % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
28         printf %02x $$(( ( $$RLEN / 256 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
29         printf %02x $$(( ( $$RLEN / 65536 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
30         printf %02x $$(( ( $$RLEN / 16777216 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
31         cat $(COMPONENT_PATH)/../build/romfs.img >>$(COMPONENT_PATH)/../build/$$DIRNAME.bin ; \
32         LEN=$$(stat -c %s $(COMPONENT_PATH)/../build/$$DIRNAME.bin) ;\
33         UNIXTIME=$$(date +%s | tr -d '\n') ; \
34         echo -n -e "{\r\n \"schema\": \"lws1\",\r\n \"model\": \"$(CONFIG_LWS_MODEL_NAME)\",\r\n \"builder\": \"" > $(jbi) ;\
35         hostname | tr -d '\n' >> $(jbi) ;\
36         echo -n -e "\",\r\n \"app\": \"" >> $(jbi) ;\
37         echo -n $$DIRNAME >> $(jbi) ;\
38         echo -n -e "\",\r\n \"user\": \"" >> $(jbi) ;\
39         whoami | tr -d '\n' >>$(jbi) ;\
40         echo -n -e  "\",\r\n \"git\": \"" >> $(jbi) ;\
41         git describe --dirty --always | tr -d '\n' >> $(jbi) ;\
42         echo -n -e  "\",\r\n \"date\": \"" >> $(jbi) ;\
43         date | tr -d '\n' >> $(jbi) ;\
44         echo -n -e "\",\r\n \"unixtime\": \"" >> $(jbi) ;\
45         echo -n $$UNIXTIME >> $(jbi) ;\
46         echo -n -e "\",\r\n \"file\": \""$$DIRNAME-$$UNIXTIME.bin >> $(jbi) ;\
47         echo -n -e "\",\r\n \"factory\": \"$(FAC)" >> $(jbi) ;\
48         echo -n -e "\"\r\n}"  >> $(jbi) ;\
49         JLEN=$$(stat -c %s $(jbi)) ;\
50         printf %02x $$(( $$JLEN % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
51         printf %02x $$(( ( $$JLEN / 256 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
52         printf %02x $$(( ( $$JLEN / 65536 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
53         printf %02x $$(( ( $$JLEN / 16777216 ) % 256 )) | xxd -r -p >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
54         cat $(jbi) >> $(COMPONENT_PATH)/../build/$$DIRNAME.bin ;\
55         LEN=$$(stat -c %s $(COMPONENT_PATH)/../build/$$DIRNAME.bin) ;\
56         cp $(COMPONENT_PATH)/../build/$$DIRNAME.bin $(COMPONENT_PATH)/../build/$$DIRNAME-$$UNIXTIME.bin ;\
57         printf "    After ROMFS + Build info: 0x%06x (%8d)\n" $$LEN $$LEN
58
59 .PHONY: manifest
60 manifest:
61 ifeq ($F,)
62         echo "Usage make F=<factory app dir> A=<app dir> manifest"
63         exit 1
64 endif
65 ifeq ($A,)
66         echo "Usage make F=<factory app dir> A=<app dir> manifest"
67         exit 1
68 endif
69         echo -n -e "{\r\n\"app\": " > build/manifest.json
70         cat $(A)/build/json-buildinfo >> build/manifest.json
71         echo -n -e ", \"factory\": " >> build/manifest.json
72         cat $(F)/build/json-buildinfo >> build/manifest.json
73         echo -n -e "\r\n}\r\n" >> build/manifest.json
74
75 all: pack.img
76
77 flash_ota:
78         DIRNAME=$$(basename $$(pwd) | tr -d '\n') ;\
79         $(IDF_PATH)/components/esptool_py/esptool/esptool.py \
80                 --chip esp32 \
81                 --port $(CONFIG_ESPTOOLPY_PORT) \
82                 --baud $(CONFIG_ESPTOOLPY_BAUD) \
83                 write_flash 0x110000 $(COMPONENT_PATH)/../build/$$DIRNAME.bin
84
85 erase_ota:
86         $(IDF_PATH)/components/esptool_py/esptool/esptool.py \
87                 --chip esp32 \
88                 --port $(CONFIG_ESPTOOLPY_PORT) \
89                 --baud $(CONFIG_ESPTOOLPY_BAUD) \
90                 erase_region 0x110000 0x2f0000
91
92
93 export A
94 export F
95 .PHONY: upload
96 upload: manifest
97 ifeq ($F,)
98         echo "Usage make F=<factory app dir> A=<app dir> manifest"
99         exit 1
100 endif
101 ifeq ($A,)
102         echo "Usage make F=<factory app dir> A=<app dir> manifest"
103         exit 1
104 endif
105         UPL=$(CONFIG_LWS_OTA_SERVER_UPLOAD_USER)@$(CONFIG_LWS_OTA_SERVER_FQDN):$(CONFIG_LWS_OTA_SERVER_UPLOAD_PATH)/$(CONFIG_LWS_OTA_SERVER_BASE_URL)/$(CONFIG_LWS_MODEL_NAME)/ ;\
106         AFILE=$(A)/build/$$(cat $$A/build/json-buildinfo | grep -- \"file\"\: |cut -d' ' -f3 |cut -d'"' -f2) ;\
107         echo "  Uploading $$AFILE to " $$UPL ;\
108         scp $$AFILE $$UPL ;\
109         FFILE=$(F)/build/$$(cat $$F/build/json-buildinfo | grep -- \"file\"\: |cut -d' ' -f3 |cut -d'"' -f2) ;\
110         echo "  Uploading $$FFILE" ;\
111         scp  $$FFILE $$UPL ;\
112         echo "  Uploading manifest" ;\
113         scp build/manifest.json $$UPL