File: update-data-region.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites:
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (1109 lines) | stat: -rw-r--r-- 37,704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
(in-package "ACL2")

;  update-data-region.lisp                              Mihir Mehta

; This is optimised code for updating the data region from a string, along with
; necessary basic definitions.

(include-book "fat32-in-memory")
(include-book "../utilities/cluster-listp")
(include-book "../utilities/member-intersectp")

(local (include-book "rtl/rel9/arithmetic/top" :dir :system))

;; These are some rules from other books which are either interacting badly
;; with the theory I've built up so far, or else causing a lot of unnecessary
;; frames and tries.
(local
 (in-theory (disable take-of-too-many make-list-ac-removal
                     revappend-removal)))

(local
 (in-theory (disable read-file-into-string1 nth update-nth ceiling floor
                     mod true-listp)))

(defund
  cluster-size (fat32$c)
  (declare (xargs :stobjs fat32$c
                  :guard (fat32$c-p fat32$c)))
  (mbe :exec (* (bpb_secperclus fat32$c)
                (bpb_bytspersec fat32$c))
       :logic (nfix (* (bpb_secperclus fat32$c)
                       (bpb_bytspersec fat32$c)))))

(defthm natp-of-cluster-size
  (implies (fat32$c-p fat32$c)
           (natp (cluster-size fat32$c)))
  :hints (("goal" :in-theory (enable fat32$c-p cluster-size
                                     bpb_bytspersec bpb_secperclus)))
  :rule-classes ((:rewrite
                  :corollary
                  (implies (fat32$c-p fat32$c)
                           (integerp (cluster-size fat32$c))))
                 (:rewrite
                  :corollary
                  (implies (fat32$c-p fat32$c)
                           (rationalp (cluster-size fat32$c))))
                 (:linear
                  :corollary
                  (implies (fat32$c-p fat32$c)
                           (<= 0 (cluster-size fat32$c))))
                 (:rewrite
                  :corollary
                  (implies (fat32$c-p fat32$c)
                           (equal
                           (nfix (cluster-size fat32$c))
                           (cluster-size fat32$c))))))

(defthm
  cluster-size-of-update-nth
  (implies
   (not (member-equal key
                      (list *bpb_secperclus* *bpb_bytspersec*)))
   (equal (cluster-size (update-nth key val fat32$c))
          (cluster-size fat32$c)))
  :hints (("goal" :in-theory (enable cluster-size))))

(defthm
  cluster-size-of-resize-data-region
  (equal (cluster-size (resize-data-region i fat32$c))
         (cluster-size fat32$c))
  :hints (("goal" :in-theory (enable resize-data-region))))

(defthm
  cluster-size-of-resize-fat
  (equal (cluster-size (resize-fat i fat32$c))
         (cluster-size fat32$c))
  :hints (("goal" :in-theory (enable resize-fat))))

(defund
  count-of-clusters (fat32$c)
  (declare
   (xargs :stobjs fat32$c
          :guard (and (fat32$c-p fat32$c)
                      (>= (bpb_secperclus fat32$c) 1))))
  (floor (- (bpb_totsec32 fat32$c)
            (+ (bpb_rsvdseccnt fat32$c)
               (* (bpb_numfats fat32$c)
                  (bpb_fatsz32 fat32$c))))
         (bpb_secperclus fat32$c)))

(defthm
  count-of-clusters-of-resize-fat
  (equal (count-of-clusters (resize-fat i fat32$c))
         (count-of-clusters fat32$c))
  :hints (("goal" :in-theory (enable count-of-clusters))))

(defthm
  count-of-clusters-of-update-nth
  (implies
   (not (member key
                (list *bpb_totsec32*
                      *bpb_rsvdseccnt* *bpb_numfats*
                      *bpb_fatsz32* *bpb_secperclus*)))
   (equal
    (count-of-clusters (update-nth key val fat32$c))
    (count-of-clusters fat32$c)))
  :hints (("goal" :in-theory (enable count-of-clusters))))

(defthm
  count-of-clusters-of-update-data-regioni
  (equal
   (count-of-clusters (update-data-regioni i v fat32$c))
   (count-of-clusters fat32$c))
  :hints
  (("goal"
    :in-theory (enable update-data-regioni))))

(defun
  stobj-cluster-listp-helper
  (fat32$c n)
  (declare
   (xargs
    :stobjs fat32$c
    :guard (and (fat32$c-p fat32$c)
                (natp n)
                (<= n (data-region-length fat32$c)))
    :guard-hints
    (("goal" :in-theory (disable fat32$c-p)))))
  (or
   (zp n)
   (let
    ((current-cluster
      (data-regioni (- (data-region-length fat32$c)
                       n)
                    fat32$c)))
    (and
     (cluster-p current-cluster
                (cluster-size fat32$c))
     (stobj-cluster-listp-helper fat32$c (- n 1))))))

(defthm
  stobj-cluster-listp-helper-correctness-1
  (implies
   (and (natp n)
        (<= n (data-region-length fat32$c)))
   (equal
    (stobj-cluster-listp-helper fat32$c n)
    (cluster-listp
     (nthcdr
      (- (data-region-length fat32$c)
         n)
      (true-list-fix (nth *data-regioni* fat32$c)))
     (cluster-size fat32$c))))
  :hints
  (("goal"
    :in-theory (e/d (data-regioni data-region-length
                                  nth)
                    ;; Try disabling rules until the proof works again...
                    ((:rewrite a1)
                     (:rewrite a2)
                     (:rewrite a4)
                     (:rewrite a9)))
    :induct (stobj-cluster-listp-helper fat32$c n)
    :expand
    ((true-list-fix (nth *data-regioni* fat32$c))
     (cluster-listp
      (nthcdr
       (+ (- n)
          (len (nth *data-regioni* fat32$c)))
       (true-list-fix (nth *data-regioni* fat32$c)))
      (cluster-size fat32$c))
     (cluster-listp
      (nthcdr
       (+ (- n)
          (len (cdr (nth *data-regioni* fat32$c))))
       (true-list-fix
        (cdr (nth *data-regioni* fat32$c))))
      (cluster-size fat32$c)))))
  :rule-classes
  ((:rewrite
    :corollary
    (implies
     (and (natp n)
          (<= n (data-region-length fat32$c))
          (fat32$c-p fat32$c))
     (equal (stobj-cluster-listp-helper fat32$c n)
            (cluster-listp
             (nthcdr (- (data-region-length fat32$c)
                        n)
                     (nth *data-regioni* fat32$c))
             (cluster-size fat32$c))))
    :hints (("goal" :in-theory (enable fat32$c-p))))))

(defund
  fat-entry-count (fat32$c)
  (declare (xargs :guard (fat32$c-p fat32$c)
                  :stobjs fat32$c))
  (floor (* (bpb_fatsz32 fat32$c)
            (bpb_bytspersec fat32$c))
         4))

(defthm
  fat-entry-count-of-update-nth
  (implies
   (not (member-equal key
                      (list *bpb_fatsz32* *bpb_bytspersec*)))
   (equal (fat-entry-count (update-nth key val fat32$c))
          (fat-entry-count fat32$c)))
  :hints
  (("goal" :in-theory (enable fat-entry-count
                              bpb_bytspersec bpb_fatsz32))))

(defthm
  fat-entry-count-of-resize-data-region
  (equal (fat-entry-count
          (resize-data-region i fat32$c))
         (fat-entry-count fat32$c))
  :hints (("goal" :in-theory (enable resize-data-region))))

(defthm
  fat32-entry-p-of-bpb_rootclus-when-fat32$c-p
  (implies (fat32$c-p fat32$c)
           (fat32-entry-p (bpb_rootclus fat32$c)))
  :hints (("goal" :in-theory (enable fat32-entry-p))))

(defthm
  fat-entry-count-of-update-fati
  (equal (fat-entry-count (update-fati i v fat32$c))
         (fat-entry-count fat32$c))
  :hints
  (("goal" :in-theory (enable fat-entry-count update-fati))))

(encapsulate
  ()

  ;; Trying to remove this is hazardous, even though it doesn't seem to be used
  ;; anywhere!
  (local
   (defthm
     lemma-1
     (implies (and
               (fat32$c-p fat32$c)
               (>= (bpb_bytspersec fat32$c) *ms-min-bytes-per-sector*)
               (>= (bpb_secperclus fat32$c) 1))
              (not (equal (cluster-size fat32$c)
                          0)))
     :hints (("goal" :in-theory (enable cluster-size)))))

  (defund lofat-fs-p (fat32$c)
    (declare (xargs :stobjs fat32$c :guard t))
    (and (fat32$c-p fat32$c)
         (>= (bpb_bytspersec fat32$c)
             *ms-min-bytes-per-sector*)
         (>= (bpb_secperclus fat32$c) 1)
         (>= (count-of-clusters fat32$c)
             *ms-min-count-of-clusters*)
         (<= (+ *ms-first-data-cluster*
                (count-of-clusters fat32$c))
             *ms-bad-cluster*)
         (>= (bpb_rsvdseccnt fat32$c) 1)
         (>= (bpb_numfats fat32$c) 1)
         (>= (bpb_fatsz32 fat32$c) 1)
         ;; These constraints on bpb_rootclus aren't in the spec, but they are
         ;; clearly implied
         (>= (fat32-entry-mask (bpb_rootclus fat32$c))
             *ms-first-data-cluster*)
         (< (fat32-entry-mask (bpb_rootclus fat32$c))
            (+ *ms-first-data-cluster*
               (count-of-clusters fat32$c)))
         (<= (+ (count-of-clusters fat32$c)
                *ms-first-data-cluster*)
             (fat-entry-count fat32$c))
         ;; The spec (page 9) imposes both hard and soft limits on the legal
         ;; values of the cluster size, limiting it to being a power of 2 from
         ;; 512 through 32768. The following two clauses, however, are less
         ;; stringent - they allow values of cluster size which are powers of 2
         ;; going up to 2097152, although the lower bound of 512 is retained
         ;; thanks to the lower bounds on bpb_bytspersec and bpb_secperclus
         ;; above.
         (equal (mod (cluster-size fat32$c)
                     *ms-d-e-length*)
                0)
         (equal (mod *ms-max-dir-size*
                     (cluster-size fat32$c))
                0)
         ;; Some array properties in addition to the scalar properties
         (stobj-cluster-listp-helper
          fat32$c
          (data-region-length fat32$c))
         (equal (data-region-length fat32$c)
                (count-of-clusters fat32$c))
         (equal (* 4 (fat-length fat32$c))
                (* (bpb_fatsz32 fat32$c)
                   (bpb_bytspersec fat32$c)))))

  (local
   (defthm
     lemma-2
     (implies (and (fat32$c-p fat32$c)
                   (< 0 (bpb_bytspersec fat32$c)))
              (< (fat-entry-count fat32$c)
                 (ash 1 48)))
     :rule-classes ()
     :hints (("goal"
              :do-not-induct t
              :in-theory
              (enable fat32$c-p fat-entry-count
                      bpb_bytspersec bpb_fatsz32)))))

  (defthm
    lofat-fs-p-correctness-1
    (implies (lofat-fs-p fat32$c)
             (and (fat32$c-p fat32$c)
                  (integerp (cluster-size fat32$c))
                  (>= (cluster-size fat32$c)
                      *ms-min-bytes-per-sector*)
                  (>= (count-of-clusters fat32$c)
                      *ms-min-count-of-clusters*)
                  (equal (mod (cluster-size fat32$c)
                              *ms-d-e-length*)
                         0)
                  (equal (mod *ms-max-dir-size*
                              (cluster-size fat32$c))
                         0)
                  (<= (+ *ms-first-data-cluster*
                         (count-of-clusters fat32$c))
                      *ms-bad-cluster*)
                  (>= (bpb_secperclus fat32$c) 1)
                  (>= (bpb_rsvdseccnt fat32$c) 1)
                  (>= (bpb_numfats fat32$c) 1)
                  (>= (bpb_fatsz32 fat32$c) 1)
                  (>= (fat32-entry-mask (bpb_rootclus fat32$c))
                      *ms-first-data-cluster*)
                  ;; There was a bug here, which we fixed - previously,
                  ;; bpb_rootclus was only allowed to point at clusters up to
                  ;; but not including (count-of-clusters fat32$c),
                  ;; which causes two clusters (up to but not including
                  ;; (+ 2 (count-of-clusters fat32$c))) to be left out.
                  (< (fat32-entry-mask (bpb_rootclus fat32$c))
                     (+ *ms-first-data-cluster*
                        (count-of-clusters fat32$c)))
                  (>= (bpb_bytspersec fat32$c)
                      *ms-min-bytes-per-sector*)
                  (equal (data-region-length fat32$c)
                         (count-of-clusters fat32$c))
                  (<= (+ (count-of-clusters fat32$c)
                         *ms-first-data-cluster*)
                      (fat-length fat32$c))
                  (equal (fat-length fat32$c)
                         (fat-entry-count fat32$c))
                  ;; This also represents a fixed bug - earlier, we were going
                  ;; to return an error for all filesystems with fat-length
                  ;; greater than *ms-bad-cluster*. The upper limit is actually
                  ;; (ash 1 28) - only slightly greater than *ms-bad-cluster* -
                  ;; derived from bpb_fatsz32 being up to (ash 1 16) and
                  ;; bpb_bytspersec being up to 4096.
                  (< (fat-entry-count fat32$c)
                     (ash 1 48))))
    :hints
    (("goal"
      :in-theory
      (e/d
       (lofat-fs-p cluster-size fat-entry-count)
       (fat32$c-p))
      :use
      lemma-2))
    :rule-classes
    ((:rewrite
      :corollary
      (implies (lofat-fs-p fat32$c)
               (and (fat32$c-p fat32$c)
                    (integerp (cluster-size fat32$c))
                    (equal (mod (cluster-size fat32$c)
                                *ms-d-e-length*)
                           0)
                    (equal (mod *ms-max-dir-size*
                                (cluster-size fat32$c))
                           0)
                    (equal (data-region-length fat32$c)
                           (count-of-clusters fat32$c))
                    (equal (fat-length fat32$c)
                           (fat-entry-count fat32$c)))))
     (:forward-chaining
      :corollary
      (implies (lofat-fs-p fat32$c)
               (integerp (cluster-size fat32$c))))
     (:linear
      :corollary
      (implies
       (lofat-fs-p fat32$c)
       (and (>= (cluster-size fat32$c)
                *ms-min-bytes-per-sector*)
            (>= (count-of-clusters fat32$c)
                *ms-min-count-of-clusters*)
            (<= (+ *ms-first-data-cluster*
                   (count-of-clusters fat32$c))
                *ms-bad-cluster*)
            (>= (bpb_secperclus fat32$c) 1)
            (>= (bpb_rsvdseccnt fat32$c) 1)
            (>= (bpb_numfats fat32$c) 1)
            (>= (bpb_fatsz32 fat32$c) 1)
            (>= (fat32-entry-mask (bpb_rootclus fat32$c))
                *ms-first-data-cluster*)
            (< (fat32-entry-mask (bpb_rootclus fat32$c))
               (+ *ms-first-data-cluster*
                  (count-of-clusters fat32$c)))
            (>= (bpb_bytspersec fat32$c)
                *ms-min-bytes-per-sector*)
            (>= (* (cluster-size fat32$c)
                   (count-of-clusters fat32$c))
                (* *ms-min-bytes-per-sector*
                   *ms-min-count-of-clusters*))
            (<= (+ (count-of-clusters fat32$c)
                   *ms-first-data-cluster*)
                (fat-entry-count fat32$c))
            (< (fat-entry-count fat32$c)
               (ash 1 48))))))))

(defthm
  fati-when-lofat-fs-p
  (implies (lofat-fs-p fat32$c)
           (equal (fat32-entry-p (fati i fat32$c))
                  (< (nfix i) (fat-length fat32$c))))
  :hints (("goal" :in-theory (enable lofat-fs-p fat32$c-p fati fat-length)))
  :rule-classes
  (:rewrite
   (:rewrite :corollary (implies (lofat-fs-p fat32$c)
                                 (equal (unsigned-byte-p 32 (fati i fat32$c))
                                        (< (nfix i) (fat-length fat32$c))))
             :hints (("goal" :do-not-induct t
                      :in-theory (enable fat32-entry-p))))))

(defthm
  cluster-size-of-update-fati
  (equal (cluster-size (update-fati i v fat32$c))
         (cluster-size fat32$c))
  :hints
  (("goal" :in-theory (enable cluster-size update-fati))))

(defthm
  count-of-clusters-of-update-fati
  (equal (count-of-clusters (update-fati i v fat32$c))
         (count-of-clusters fat32$c))
  :hints
  (("goal"
    :in-theory (enable count-of-clusters update-fati bpb_totsec32))))

;; After disabling, this function used to cause  9419883 frames and 76526
;; tries. So we went back to leaving it enabled and those numbers are now
;; 9446133 and 82621 respectively.
(defthm
  lofat-fs-p-of-update-fati
  (implies (and (lofat-fs-p fat32$c)
                (case-split (< i (fat-length fat32$c))))
           (equal (lofat-fs-p (update-fati i v fat32$c))
                  (fat32-entry-p v)))
  :hints
  (("goal"
    :in-theory
    '((:compound-recognizer natp-compound-recognizer)
      (:compound-recognizer true-listp-when-fat32-entry-list-p-compound-recognizer)
      (:definition bpb_bkbootsecp)
      (:definition bpb_bytspersecp)
      (:definition bpb_extflagsp)
      (:definition bpb_fatsz16p)
      (:definition bpb_fatsz32p)
      (:definition bpb_fsinfop)
      (:definition bpb_fsver_majorp)
      (:definition bpb_fsver_minorp)
      (:definition bpb_hiddsecp)
      (:definition bpb_mediap)
      (:definition bpb_numfatsp)
      (:definition bpb_numheadsp)
      (:definition bpb_reservedp-alt)
      (:definition bpb_rootclusp)
      (:definition bpb_rootentcntp)
      (:definition bpb_rsvdseccntp)
      (:definition bpb_secperclusp)
      (:definition bpb_secpertrkp)
      (:definition bpb_totsec16p)
      (:definition bpb_totsec32p)
      (:definition bs_bootsigp)
      (:definition bs_drvnump)
      (:definition bs_filsystypep-alt)
      (:definition bs_jmpbootp-alt)
      (:definition bs_oemnamep-alt)
      (:definition bs_reserved1p)
      (:definition bs_volidp)
      (:definition bs_vollabp-alt)
      (:definition count-of-clusters)
      (:definition data-region-length)
      (:definition data-regionp-alt)
      (:definition fat-length)
      (:definition fat32$c-p)
      (:definition fatp-alt)
      (:definition force)
      (:definition integer-range-p)
      (:definition length)
      (:definition lofat-fs-p)
      (:definition max)
      (:definition nfix)
      (:definition not)
      (:definition nthcdr)
      (:definition unsigned-byte-p)
      (:definition update-fati)
      (:definition update-nth-array)
      (:executable-counterpart binary-+)
      (:executable-counterpart equal)
      (:executable-counterpart expt)
      (:executable-counterpart integerp)
      (:executable-counterpart max)
      (:executable-counterpart member-equal)
      (:executable-counterpart nfix)
      (:executable-counterpart not)
      (:executable-counterpart zp)
      (:rewrite bpb_bytspersec-of-update-nth . 23)
      (:rewrite bpb_fatsz32-of-update-nth . 23)
      (:rewrite bpb_numfats-of-update-nth . 23)
      (:rewrite bpb_rootclus-of-update-nth . 23)
      (:rewrite bpb_rsvdseccnt-of-update-nth . 23)
      (:rewrite bpb_secperclus-of-update-nth . 23)
      (:rewrite cluster-size-of-update-nth)
      (:rewrite commutativity-2-of-+)
      (:rewrite commutativity-of-*)
      (:rewrite commutativity-of-+)
      (:rewrite count-of-clusters-of-update-nth)
      (:rewrite distributivity-of-minus-over-+)
      (:rewrite fat-entry-count-of-update-nth)
      (:rewrite fat32-entry-list-p-of-update-nth)
      (:rewrite inverse-of-+)
      (:rewrite len-update-nth)
      (:rewrite nth-update-nth)
      (:rewrite stobj-cluster-listp-helper-correctness-1)
      (:type-prescription cluster-listp)
      (:type-prescription count-of-clusters)
      (:type-prescription fat-entry-count)
      (:type-prescription fat32-entry-mask)
      (:type-prescription fat32-entry-p)
      (:type-prescription floor)
      (:type-prescription len)
      (:type-prescription string-listp)
      (:type-prescription true-listp-update-nth)
      (:type-prescription unsigned-byte-listp)
      (:type-prescription update-nth))
    :use cluster-size-of-update-fati)))

(defthm
  data-regioni-when-lofat-fs-p
  (implies (and (lofat-fs-p fat32$c)
                (< (nfix i)
                   (data-region-length fat32$c)))
           (cluster-p (data-regioni i fat32$c)
                      (cluster-size fat32$c)))
  :hints
  (("goal" :in-theory (e/d (lofat-fs-p
                            fat32$c-p
                            data-regioni data-region-length)
                           (unsigned-byte-p))))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (and (lofat-fs-p fat32$c)
          (< (nfix i)
             (data-region-length fat32$c)))
     (and
      (stringp (data-regioni i fat32$c))
      (equal (len (explode (data-regioni i fat32$c)))
             (cluster-size fat32$c))))
    :hints (("goal" :in-theory (enable cluster-p))))))

(defthm
  cluster-size-of-update-data-regioni
  (equal
   (cluster-size (update-data-regioni i v fat32$c))
   (cluster-size fat32$c))
  :hints
  (("goal"
    :in-theory (enable cluster-size update-data-regioni))))

(defthm
  lofat-fs-p-of-update-data-regioni
  (implies
   (and (lofat-fs-p fat32$c)
        (< i (data-region-length fat32$c)))
   (equal (lofat-fs-p (update-data-regioni i v fat32$c))
          (cluster-p v (cluster-size fat32$c))))
  :hints
  (("goal"
    :do-not-induct t
    :in-theory
    '((:compound-recognizer natp-compound-recognizer)
      (:compound-recognizer true-listp-when-fat32-entry-list-p-compound-recognizer)
      (:definition bpb_bkbootsecp)
      (:definition bpb_bytspersecp)
      (:definition bpb_extflagsp)
      (:definition bpb_fatsz16p)
      (:definition bpb_fatsz32p)
      (:definition bpb_fsinfop)
      (:definition bpb_fsver_majorp)
      (:definition bpb_fsver_minorp)
      (:definition bpb_hiddsecp)
      (:definition bpb_mediap)
      (:definition bpb_numfatsp)
      (:definition bpb_numheadsp)
      (:definition bpb_reservedp-alt)
      (:definition bpb_rootclusp)
      (:definition bpb_rootentcntp)
      (:definition bpb_rsvdseccntp)
      (:definition bpb_secperclusp)
      (:definition bpb_secpertrkp)
      (:definition bpb_totsec16p)
      (:definition bpb_totsec32p)
      (:definition bs_bootsigp)
      (:definition bs_drvnump)
      (:definition bs_filsystypep-alt)
      (:definition bs_jmpbootp-alt)
      (:definition bs_oemnamep-alt)
      (:definition bs_reserved1p)
      (:definition bs_volidp)
      (:definition bs_vollabp-alt)
      (:definition count-of-clusters)
      (:definition data-region-length)
      (:definition data-regionp-alt)
      (:definition fat-length)
      (:definition fat32$c-p)
      (:definition fatp-alt)
      (:definition fix)
      (:definition integer-range-p)
      (:definition length)
      (:definition lofat-fs-p)
      (:definition max)
      (:definition nfix)
      (:definition not)
      (:definition nthcdr)
      (:definition unsigned-byte-p)
      (:definition update-data-regioni)
      (:definition update-nth-array)
      (:executable-counterpart binary-+)
      (:executable-counterpart equal)
      (:executable-counterpart expt)
      (:executable-counterpart integerp)
      (:executable-counterpart max)
      (:executable-counterpart member-equal)
      (:executable-counterpart nfix)
      (:executable-counterpart not)
      (:executable-counterpart unary--)
      (:executable-counterpart zp)
      (:rewrite bpb_bytspersec-of-update-nth . 23)
      (:rewrite bpb_fatsz32-of-update-nth . 23)
      (:rewrite bpb_numfats-of-update-nth . 23)
      (:rewrite bpb_rootclus-of-update-nth . 23)
      (:rewrite bpb_rsvdseccnt-of-update-nth . 23)
      (:rewrite bpb_secperclus-of-update-nth . 23)
      (:rewrite cluster-listp-of-update-nth)
      (:rewrite cluster-p-correctness-1)
      (:rewrite cluster-size-of-update-nth)
      (:rewrite commutativity-2-of-+)
      (:rewrite commutativity-of-*)
      (:rewrite commutativity-of-+)
      (:rewrite count-of-clusters-of-update-nth)
      (:rewrite distributivity-of-minus-over-+)
      (:rewrite fat-entry-count-of-update-nth)
      (:rewrite fat-length-of-update-nth)
      (:rewrite inverse-of-+)
      (:rewrite len-update-nth)
      (:rewrite nth-update-nth)
      (:rewrite nthcdr-of-update-nth)
      (:rewrite stobj-cluster-listp-helper-correctness-1)
      (:rewrite string-listp-of-update-nth)
      (:rewrite unicity-of-0)
      (:type-prescription cluster-listp)
      (:type-prescription cluster-p)
      (:type-prescription count-of-clusters)
      (:type-prescription fat-entry-count)
      (:type-prescription fat32-entry-mask)
      (:type-prescription floor)
      (:type-prescription len)
      (:type-prescription max)
      (:type-prescription string-listp)
      (:type-prescription true-listp-update-nth)
      (:type-prescription unsigned-byte-listp)
      (:type-prescription update-nth))
    :use cluster-size-of-update-data-regioni)))

(defun
    update-data-region
    (fat32$c str len)
  (declare
   (xargs
    :guard (and (stringp str)
                (natp len)
                (<= len
                    (data-region-length fat32$c))
                (>= (length str)
                    (* (- (data-region-length fat32$c)
                          len)
                       (cluster-size fat32$c)))
                (<= len
                    (- *ms-bad-cluster*
                       *ms-first-data-cluster*)))
    :stobjs fat32$c
    :measure (nfix len)))
  (b*
      ((len (the (unsigned-byte 28) len))
       ((when (zp len)) (mv fat32$c 0))
       (cluster-size (cluster-size fat32$c))
       (index (- (data-region-length fat32$c)
                 len)))
    (if
        (<= (* (+ index 1) cluster-size)
            (length str))
        (b*
            ((current-cluster (subseq str (* index cluster-size)
                                      (* (+ index 1) cluster-size)))
             (fat32$c
              (update-data-regioni
               index current-cluster fat32$c)))
          (update-data-region
           fat32$c
           str (the (unsigned-byte 28) (- len 1))))
      (b*
          ((current-cluster (subseq str (* index cluster-size) nil))
           (fat32$c
            (update-data-regioni
             index current-cluster fat32$c)))
        (mv fat32$c *eio*)))))

(defun
    update-data-region-from-disk-image
    (fat32$c len state tmp_init image-path)
  (declare
   (xargs
    :guard
    (and (natp tmp_init)
         (stringp image-path)
         (stringp (read-file-into-string image-path))
         (natp len)
         (<= len
             (data-region-length fat32$c))
         (>= (length (read-file-into-string image-path))
             (+ tmp_init
                (* (- (data-region-length fat32$c)
                      len)
                   (cluster-size fat32$c))))
         (<= len
             (- *ms-bad-cluster*
                *ms-first-data-cluster*)))
    :stobjs (fat32$c state)
    :measure (nfix len)))
  (b*
      ((len (the (unsigned-byte 28) len))
       ((when (zp len)) (mv fat32$c 0))
       (cluster-size (cluster-size fat32$c))
       (index (- (data-region-length fat32$c)
                 len))
       (fat32$c
        (update-data-regioni
         index
         (read-file-into-string
          image-path
          :start (+ tmp_init (* index cluster-size))
          :bytes cluster-size)
         fat32$c)))
    (if (equal (length (data-regioni index fat32$c))
               cluster-size)
        (update-data-region-from-disk-image
         fat32$c
         (the (unsigned-byte 28) (- len 1))
         state tmp_init image-path)
      (mv fat32$c *eio*))))

(defthm
  update-data-region-from-disk-image-correctness-1
  (implies
   (and (natp tmp_init)
        (<= len
            (data-region-length fat32$c))
        (>= (length (read-file-into-string image-path))
            (+ tmp_init
               (* (- (data-region-length fat32$c)
                     len)
                  (cluster-size fat32$c))))
        (not (zp (cluster-size fat32$c))))
   (equal (update-data-region-from-disk-image fat32$c
                                              len state tmp_init image-path)
          (update-data-region fat32$c
                              (subseq (read-file-into-string image-path)
                                      tmp_init nil)
                              len)))
  :hints
  (("goal"
    :induct (update-data-region-from-disk-image fat32$c
                                                len state tmp_init image-path)
    :in-theory (enable take-of-nthcdr)
    :expand (:free (fat32$c str)
                   (update-data-region fat32$c str len)))))

(defthm
  fat32$c-p-of-update-data-regioni
  (implies
   (fat32$c-p fat32$c)
   (equal
    (fat32$c-p (update-data-regioni i v fat32$c))
    (and (stringp v)
         (<= (nfix i)
             (data-region-length fat32$c)))))
  :hints
  (("goal"
    :in-theory (enable fat32$c-p update-data-regioni
                       data-region-length))))

(defthm
  fat32$c-p-of-update-data-region
  (implies (and (fat32$c-p fat32$c)
                (stringp str))
           (fat32$c-p
            (mv-nth 0
                    (update-data-region fat32$c str len)))))

(defthm
  bpb_bytspersec-of-update-data-region
  (equal
   (bpb_bytspersec (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_bytspersec fat32$c)))

(defthm
  bpb_secperclus-of-update-data-region
  (equal
   (bpb_secperclus (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_secperclus fat32$c)))

(defthm
  bpb_rsvdseccnt-of-update-data-region
  (equal
   (bpb_rsvdseccnt (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_rsvdseccnt fat32$c)))

(defthm
  bpb_totsec32-of-update-data-region
  (equal
   (bpb_totsec32 (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_totsec32 fat32$c)))

(defthm
  bpb_fatsz32-of-update-data-region
  (equal
   (bpb_fatsz32 (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_fatsz32 fat32$c)))

(defthm
  bpb_numfats-of-update-data-region
  (equal
   (bpb_numfats (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_numfats fat32$c)))

(defthm
  bpb_rootclus-of-update-data-region
  (equal
   (bpb_rootclus (mv-nth 0 (update-data-region fat32$c str len)))
   (bpb_rootclus fat32$c)))

(defthm
  fat-length-of-update-data-region
  (equal
   (fat-length (mv-nth 0 (update-data-region fat32$c str len)))
   (fat-length fat32$c)))

(defthm
  fat-entry-count-of-update-data-region
  (equal (fat-entry-count
          (mv-nth 0 (update-data-region fat32$c str len)))
         (fat-entry-count fat32$c))
  :hints (("goal" :in-theory (enable fat-entry-count))))

(defthm
  data-region-length-of-update-data-region
  (implies
   (<= len
       (data-region-length fat32$c))
   (equal (data-region-length
           (mv-nth 0 (update-data-region fat32$c str len)))
          (data-region-length fat32$c)))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (<= len
         (data-region-length fat32$c))
     (equal
      (consp (nth *data-regioni*
                  (mv-nth 0 (update-data-region fat32$c str len))))
      (consp (nth *data-regioni* fat32$c))))
    :hints
    (("goal"
      :in-theory (enable data-region-length)
      :do-not-induct t
      :expand
      ((len (nth *data-regioni*
                 (mv-nth 0 (update-data-region fat32$c str len))))
       (len (nth *data-regioni* fat32$c))))))))

(defthmd
  update-data-region-correctness-2
  (equal
   (update-data-region fat32$c str len)
   (mv (mv-nth 0
               (update-data-region fat32$c str len))
       (mv-nth 1
               (update-data-region fat32$c str len)))))

(local
 (defthm
   update-data-region-correctness-1-lemma-1
   (implies (and (not (zp len))
                 (<= (* cluster-size
                        data-region-length)
                     (+ length-str
                        (* len cluster-size)))
                 (< (+ length-str
                       (* len cluster-size))
                    (+ cluster-size
                       (* cluster-size
                          data-region-length))))
            (< length-str
               (* cluster-size
                  data-region-length)))
   :hints (("goal" :in-theory (disable (:rewrite product-less-than-zero))
            :use ((:instance (:rewrite product-less-than-zero)
                             (y cluster-size)
                             (x (+ -1 len))))))))

;; It would be nice to make this a rule that just rewrites the inner (mv-nth
;; 1 ...) subexpression rather than the (equal (mv-nth 1 ...) ...)
;; expression, but that causes trouble with
;; string-to-lofat-ignore-lemma-14 in another book.
(defthm
  update-data-region-correctness-1
  (implies (and (natp len)
                (<= len
                    (data-region-length fat32$c))
                (>= (length str)
                    (* (- (data-region-length fat32$c)
                          len)
                       (cluster-size fat32$c))))
           (iff
            (equal (mv-nth 1
                           (update-data-region fat32$c str len))
                   0)
            (>= (length str)
                (* (data-region-length fat32$c)
                   (cluster-size fat32$c))))))

(encapsulate
  ()

  (local
   (defthm
     update-data-region-alt-lemma-1
     (equal (update-nth *data-regioni* val
                        (update-data-regioni i v fat32$c))
            (update-nth *data-regioni* val fat32$c))
     :hints (("goal" :in-theory (enable update-data-regioni)))))

  (local
   (defthm
     update-data-region-alt-lemma-2
     (implies (fat32$c-p fat32$c)
              (and
               (true-listp (nth *data-regioni* fat32$c))
               (equal
                (update-nth *data-regioni*
                            (nth *data-regioni* fat32$c)
                            fat32$c)
                fat32$c)))
     :hints (("goal" :in-theory (enable fat32$c-p)))))

  (local
   (defthm
     update-data-region-alt-lemma-3
     (equal
      (nth *data-regioni*
           (update-data-regioni i v fat32$c))
      (update-nth i v
                  (nth *data-regioni* fat32$c)))
     :hints (("goal" :in-theory (enable update-data-regioni)) )))

  (defthm
    update-data-region-alt-lemma-4
    (implies (and (not (zp len))
                  (< (len (explode str))
                     (+ (cluster-size fat32$c)
                        (* -1 len (cluster-size fat32$c))
                        (* (cluster-size fat32$c)
                           (len (nth *data-regioni* fat32$c)))))
                  (fat32$c-p fat32$c))
             (< (len (explode str))
                (* (cluster-size fat32$c)
                   (len (nth *data-regioni* fat32$c)))))
    :hints (("goal" :cases ((<= (+ (cluster-size fat32$c)
                                   (* -1 len (cluster-size fat32$c)))
                                0)))))

  (defthmd
    update-data-region-alt
    (implies
     (and (stringp str)
          (natp len)
          (>= (data-region-length fat32$c)
              len)
          (fat32$c-p fat32$c)
          (< 0 (cluster-size fat32$c))
          (>= (length str)
              (* (data-region-length fat32$c)
                 (cluster-size fat32$c))))
     (equal
      (update-data-region fat32$c str len)
      (mv
       (update-nth
        *data-regioni*
        (append
         (take (- (data-region-length fat32$c)
                  len)
               (nth *data-regioni* fat32$c))
         (make-clusters
          (subseq str
                  (* (- (data-region-length fat32$c)
                        len)
                     (cluster-size fat32$c))
                  (* (data-region-length fat32$c)
                     (cluster-size fat32$c)))
          (cluster-size fat32$c)))
        fat32$c)
       0)))
    :hints
    (("goal"
      :in-theory
      (e/d (data-region-length make-clusters
                               remember-that-time-with-update-nth
                               take-of-nthcdr)
           (append take))
      :induct (update-data-region fat32$c str len)))))

(defthm
  cluster-listp-after-update-data-region
  (implies
   (and
    (fat32$c-p fat32$c)
    (stringp str)
    (natp len)
    (>= (len (explode str))
        (* (cluster-size fat32$c)
           (data-region-length fat32$c)))
    (< 0 (cluster-size fat32$c))
    (cluster-listp (take (- (data-region-length fat32$c)
                            len)
                         (nth *data-regioni* fat32$c))
                   (cluster-size fat32$c))
    (>= (data-region-length fat32$c)
        len))
   (cluster-listp
    (nth *data-regioni*
         (mv-nth 0
                 (update-data-region fat32$c str len)))
    (cluster-size fat32$c)))
  :hints (("goal" :use update-data-region-alt))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (and (fat32$c-p fat32$c)
          (stringp str)
          (natp len)
          (>= (len (explode str))
              (* (cluster-size fat32$c)
                 (data-region-length fat32$c)))
          (< 0 (cluster-size fat32$c))
          (cluster-listp
           (take (- (data-region-length fat32$c)
                    len)
                 (nth *data-regioni* fat32$c))
           cluster-size)
          (>= (data-region-length fat32$c)
              len)
          (equal cluster-size
                 (cluster-size fat32$c)))
     (cluster-listp
      (nth
       *data-regioni*
       (mv-nth 0
               (update-data-region fat32$c str len)))
      cluster-size))
    :hints
    (("goal"
      :in-theory (e/d (fat32$c-p)
                      (fat32$c-p-of-update-data-region
                       (:definition update-data-region)
                       (:rewrite
                        update-data-region-correctness-1-lemma-1)
                       (:definition nthcdr)
                       (:rewrite cancel-common-factors-in-<)
                       (:rewrite rationalp-prod)
                       (:rewrite rationalp-sum)
                       (:rewrite integerp-prod)))
      :use fat32$c-p-of-update-data-region
      :do-not-induct t)))))