[OpenWrt-Devel] [PATCH] Fix for mvebu (WRT1900AC/WRT1200AC/etc) boot counter

Rob Mosher nyt-openwrt at countercultured.net
Tue Aug 18 13:17:23 EDT 2015


What's SOB?

Kaloz sent me the E4500 patch and told me less than a day to get it 
included into CC for the mvebu stuff.  I just applied it, fixed some 
very minor compile things, tested it, and sent in the patch since he 
went idle on IRC and I had to get to work after staying up all night.  I 
was going to reverse engineer the linksys recovery binary but he 
presented this instead.  I'm not trying to take any credit for this, 
just trying to get the router working properly and understood the CC 
deadline was fast approaching.  I'm new to the patch 
generation/submitting here so apologize if this stepped on any toes.

Yes, it compiles without warnings and it works.  Once it's run the boot 
counter no longer increments until a new image is flashed.  It should 
probably be renamed to something other than fixtrx as well. If you want 
to modify it properly or have suggestions on what to clean up I'm all ears.

On 8/18/2015 12:10 PM, Jonas Gorski wrote:
> On Tue, Aug 18, 2015 at 4:21 PM, Rob Mosher
> <nyt-openwrt at countercultured.net> wrote:
>> The uboot boot counter was never reset after a successful boot.
>> This patch prevents bootcmd and boot_part from becoming out of sync
>> after a sysupgrade and ensures the proper partition is booted.
>>
>> Signed-off-by: Rob Mosher <nyt-openwrt at countercultured.net>
> Hm, that code looks awfully familiar ... right, I wrote it! So what
> happened to my SOB?
>
>> ---
>>   package/system/mtd/Makefile                        |   2 +-
>>   package/system/mtd/src/Makefile                    |   1 +
>>   package/system/mtd/src/mvebu.c                     | 100 +++++++++++++++++++++
>>   .../linux/mvebu/base-files/etc/init.d/u-boot_env   |   1 +
>>   4 files changed, 103 insertions(+), 1 deletion(-)
>>   create mode 100644 package/system/mtd/src/mvebu.c
>>
>> diff --git a/package/system/mtd/Makefile b/package/system/mtd/Makefile
>> index 8d7bb44..ae1922f 100644
>> --- a/package/system/mtd/Makefile
>> +++ b/package/system/mtd/Makefile
>> @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
>>   include $(INCLUDE_DIR)/kernel.mk
>>
>>   PKG_NAME:=mtd
>> -PKG_RELEASE:=20
>> +PKG_RELEASE:=21
>>
>>   PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
>>   STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
>> diff --git a/package/system/mtd/src/Makefile b/package/system/mtd/src/Makefile
>> index 27ac339..3424dd2 100644
>> --- a/package/system/mtd/src/Makefile
>> +++ b/package/system/mtd/src/Makefile
>> @@ -10,6 +10,7 @@ obj.brcm47xx = $(obj.brcm)
>>   obj.bcm53xx = $(obj.brcm)
>>   obj.brcm63xx = imagetag.o
>>   obj.ramips = $(obj.seama)
>> +obj.mvebu = mvebu.c
> Does this even compile?
>
>>   ifdef FIS_SUPPORT
>>     obj += fis.o
>> diff --git a/package/system/mtd/src/mvebu.c b/package/system/mtd/src/mvebu.c
>> new file mode 100644
>> index 0000000..0929a97
>> --- /dev/null
>> +++ b/package/system/mtd/src/mvebu.c
>> @@ -0,0 +1,100 @@
>> +/*
>> + * GPL Header ...
> You didn't even bother inserting a proper GPL license header here.
>
>> + */
>> +
>> +
>> +
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +#include <stddef.h>
>> +#include <unistd.h>
>> +#include <fcntl.h>
>> +#include <sys/mman.h>
>> +#include <sys/stat.h>
>> +#include <endian.h>
>> +#include <string.h>
>> +#include <errno.h>
>> +
>> +#include <sys/ioctl.h>
>> +#include <mtd/mtd-user.h>
>> +
>> +#include "mtd.h"
>> +
>> +#define BOOTCOUNT_MAGIC        0x20110811
>> +
>> +struct bootcounter {
>> +       uint32_t magic;
>> +       uint32_t count;
>> +       uint32_t checksum;
>> +};
>> +
>> +static char page[2048];
>> +
>> +int mtd_fixtrx(const char *mtd, size_t offset)
>> +{
>> +       struct mtd_info_user mtd_info;
>> +       struct bootcounter *curr = (struct bootcounter *)page;
>> +       unsigned int i;
>> +       int last_count = 0;
>> +       int num_bc;
>> +       int fd;
>> +       int ret;
>> +
>> +       fd = mtd_check_open(mtd);
>> +
>> +       if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
>> +               fprintf(stderr, "failed to get mtd info!");
> or fix the missing newline here.
>
>> +               return -1;
>> +       }
>> +
>> +       num_bc = mtd_info.size / mtd_info.writesize;
>> +
>> +       for (i = 0; i < num_bc; i++) {
>> +               pread(fd, curr, sizeof(*curr), i * mtd_info.writesize);
>> +
>> +               if (curr->magic != BOOTCOUNT_MAGIC && curr->magic != 0xffffffff) {
>> +                       printf("unexpected magic %08x, bailing out\n", curr->magic);
>> +                       goto out;
>> +               }
>> +
>> +               if (curr->magic == 0xffffffff)
>> +                       break;
>> +
>> +               last_count = curr->count;
>> +       }
>> +
>> +       /* no need to do writes when last boot count is already 0 */
>> +       if (last_count == 0)
>> +               goto out;
>> +
>> +
>> +       if (i == num_bc) {
>> +               struct erase_info_user erase_info;
>> +               erase_info.start = 0;
>> +               erase_info.length = mtd_info.size;
>> +
>> +               /* erase block */
>> +               ret = ioctl(fd, MEMERASE, &erase_info);
>> +               if (ret < 0) {
>> +                       printf("failed to erase block: %i\n", ret);
>> +                       return -1;
>> +               }
>> +
>> +               i = 0;
>> +       }
>> +
>> +       memset(curr, 0xff, mtd_info.writesize);
>> +
>> +       curr->magic = BOOTCOUNT_MAGIC;
>> +       curr->count = 0;
>> +       curr->checksum = BOOTCOUNT_MAGIC;
>> +
>> +       ret = pwrite(fd, curr, mtd_info.writesize, i * mtd_info.writesize);
>> +       if (ret < 0)
>> +               printf("failed to write: %i\n", ret);
>> +       sync();
>> +out:
>> +       close(fd);
>> +
>> +       return 0;
>> +}
>> diff --git a/target/linux/mvebu/base-files/etc/init.d/u-boot_env b/target/linux/mvebu/base-files/etc/init.d/u-boot_env
>> index 82f36cb..2869dd6 100755
>> --- a/target/linux/mvebu/base-files/etc/init.d/u-boot_env
>> +++ b/target/linux/mvebu/base-files/etc/init.d/u-boot_env
>> @@ -9,6 +9,7 @@ boot() {
>>   case $(mvebu_board_name) in
>>          armada-385-linksys-caiman|armada-385-linksys-cobra|armada-xp-linksys-mamba)
>>                  fw_setenv auto_recovery off
>> +               mtd fixtrx s_env
>>                  ;;
>>   esac
>>   }
>> --
>> 2.1.4
>> _______________________________________________
>> openwrt-devel mailing list
>> openwrt-devel at lists.openwrt.org
>> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list