Commit e387efbd6535724051364fd78469ef611165e489

Authored by Oliver Metz
Committed by Tom Rini
1 parent 23ef62d741
Exists in master and in 56 other branches 8qm-imx_v2020.04_5.4.70_2.3.0, emb_lf-6.6.52-2.2.0, emb_lf_v2022.04, emb_lf_v2023.04, emb_lf_v2024.04, imx_v2015.04_4.1.15_1.0.0_ga, pitx_8mp_lf_v2020.04, smarc-8m-android-10.0.0_2.6.0, smarc-8m-android-11.0.0_2.0.0, smarc-8mp-android-11.0.0_2.0.0, smarc-emmc-imx_v2014.04_3.10.53_1.1.0_ga, smarc-emmc-imx_v2014.04_3.14.28_1.0.0_ga, smarc-imx-l5.0.0_1.0.0-ga, smarc-imx6_v2018.03_4.14.98_2.0.0_ga, smarc-imx7_v2017.03_4.9.11_1.0.0_ga, smarc-imx7_v2018.03_4.14.98_2.0.0_ga, smarc-imx_v2014.04_3.14.28_1.0.0_ga, smarc-imx_v2015.04_4.1.15_1.0.0_ga, smarc-imx_v2017.03_4.9.11_1.0.0_ga, smarc-imx_v2017.03_4.9.88_2.0.0_ga, smarc-imx_v2017.03_o8.1.0_1.3.0_8m, smarc-imx_v2018.03_4.14.78_1.0.0_ga, smarc-m6.0.1_2.1.0-ga, smarc-n7.1.2_2.0.0-ga, smarc-rel_imx_4.1.15_2.0.0_ga, smarc_8m-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8m-imx_v2019.04_4.19.35_1.1.0, smarc_8m_00d0-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8mm-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8mm-imx_v2019.04_4.19.35_1.1.0, smarc_8mm-imx_v2020.04_5.4.24_2.1.0, smarc_8mp_lf_v2020.04, smarc_8mq-imx_v2020.04_5.4.24_2.1.0, smarc_8mq_lf_v2020.04, ti-u-boot-2015.07, v2013.10, v2013.10-smarct33, v2013.10-smartmen, v2014.01, v2014.04, v2014.04-smarct33, v2014.04-smarct33-emmc, v2014.04-smartmen, v2014.07, v2014.07-smarct33, v2014.07-smartmen, v2015.07-smarct33, v2015.07-smarct33-emmc, v2015.07-smarct4x, v2016.05-dlt, v2016.05-smarct3x, v2016.05-smarct3x-emmc, v2016.05-smarct4x, v2017.01-smarct3x, v2017.01-smarct3x-emmc, v2017.01-smarct4x

fw_env: fix writing environment for mtd devices

Signed-off-by: Oliver Metz <oliver@freetz.org>
Tested-by: Luka Perkov <luka@openwrt.org>

Showing 1 changed file with 42 additions and 28 deletions Side-by-side Diff

... ... @@ -727,27 +727,39 @@
727 727 MEMGETBADBLOCK needs 64 bits */
728 728 int rc;
729 729  
730   - blocklen = DEVESIZE (dev);
  730 + /*
  731 + * For mtd devices only offset and size of the environment do matter
  732 + */
  733 + if (mtd_type == MTD_ABSENT) {
  734 + blocklen = count;
  735 + top_of_range = offset + count;
  736 + erase_len = blocklen;
  737 + blockstart = offset;
  738 + block_seek = 0;
  739 + write_total = blocklen;
  740 + } else {
  741 + blocklen = DEVESIZE(dev);
731 742  
732   - top_of_range = ((DEVOFFSET(dev) / blocklen) +
733   - ENVSECTORS (dev)) * blocklen;
  743 + top_of_range = ((DEVOFFSET(dev) / blocklen) +
  744 + ENVSECTORS(dev)) * blocklen;
734 745  
735   - erase_offset = (offset / blocklen) * blocklen;
  746 + erase_offset = (offset / blocklen) * blocklen;
736 747  
737   - /* Maximum area we may use */
738   - erase_len = top_of_range - erase_offset;
  748 + /* Maximum area we may use */
  749 + erase_len = top_of_range - erase_offset;
739 750  
740   - blockstart = erase_offset;
741   - /* Offset inside a block */
742   - block_seek = offset - erase_offset;
  751 + blockstart = erase_offset;
  752 + /* Offset inside a block */
  753 + block_seek = offset - erase_offset;
743 754  
744   - /*
745   - * Data size we actually have to write: from the start of the block
746   - * to the start of the data, then count bytes of data, and to the
747   - * end of the block
748   - */
749   - write_total = ((block_seek + count + blocklen - 1) /
750   - blocklen) * blocklen;
  755 + /*
  756 + * Data size we actually write: from the start of the block
  757 + * to the start of the data, then count bytes of data, and
  758 + * to the end of the block
  759 + */
  760 + write_total = ((block_seek + count + blocklen - 1) /
  761 + blocklen) * blocklen;
  762 + }
751 763  
752 764 /*
753 765 * Support data anywhere within erase sectors: read out the complete
... ... @@ -818,17 +830,18 @@
818 830 continue;
819 831 }
820 832  
821   - erase.start = blockstart;
822   - ioctl (fd, MEMUNLOCK, &erase);
823   - /* These do not need an explicit erase cycle */
824   - if (mtd_type != MTD_ABSENT &&
825   - mtd_type != MTD_DATAFLASH)
826   - if (ioctl (fd, MEMERASE, &erase) != 0) {
827   - fprintf (stderr, "MTD erase error on %s: %s\n",
828   - DEVNAME (dev),
829   - strerror (errno));
830   - return -1;
831   - }
  833 + if (mtd_type != MTD_ABSENT) {
  834 + erase.start = blockstart;
  835 + ioctl(fd, MEMUNLOCK, &erase);
  836 + /* These do not need an explicit erase cycle */
  837 + if (mtd_type != MTD_DATAFLASH)
  838 + if (ioctl(fd, MEMERASE, &erase) != 0) {
  839 + fprintf(stderr,
  840 + "MTD erase error on %s: %s\n",
  841 + DEVNAME(dev), strerror(errno));
  842 + return -1;
  843 + }
  844 + }
832 845  
833 846 if (lseek (fd, blockstart, SEEK_SET) == -1) {
834 847 fprintf (stderr,
... ... @@ -847,7 +860,8 @@
847 860 return -1;
848 861 }
849 862  
850   - ioctl (fd, MEMLOCK, &erase);
  863 + if (mtd_type != MTD_ABSENT)
  864 + ioctl(fd, MEMLOCK, &erase);
851 865  
852 866 processed += blocklen;
853 867 block_seek = 0;