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
|
; Copyright (C) 2017, Regents of the University of Texas
; Written by Mihir Mehta
; License: A 3-clause BSD license. See the LICENSE file distributed with ACL2.
(in-package "ACL2")
; fat32.lisp Mihir Mehta
; Utilities for FAT32.
; The following is in fat32.acl2, but we include it here as well for
; when we are doing interactive development, in order to read gl:: symbols.
(include-book "centaur/gl/portcullis" :dir :system)
(include-book "centaur/fty/top" :dir :system)
(include-book "file-system-lemmas")
(include-book "bounded-nat-listp")
(defconst *expt-2-28* (expt 2 28))
;; from page 18 of the FAT specification
(defconst *ms-end-of-clusterchain* (- *expt-2-28* 1))
;; from page 14 of the FAT specification
(defconst *ms-first-data-cluster* 2)
;; from page 18 of the FAT specification
(defconst *ms-bad-cluster* 268435447)
;; from page 15 of the FAT specification
(defconst *ms-fat32-min-count-of-clusters* 65525)
;; from page 9 of the FAT specification
(defconst *ms-min-bytes-per-sector* 512)
;; inferred - there can be as few as one sectors in a cluster
(defconst *ms-min-data-region-size* (* *ms-min-bytes-per-sector*
1
*ms-fat32-min-count-of-clusters*))
(defconst *ms-max-bytes-per-sector* 4096)
;; inferred - there can be as many as 128 sectors in a cluster
(defconst *ms-max-data-region-size* (* *ms-max-bytes-per-sector*
128
(- *ms-bad-cluster* 2)))
(defconst *ms-dir-ent-length* 32)
;; from include/uapi/asm-generic/errno-base.h
(defconst *ENOENT* 2) ;; No such file or directory
(defconst *EIO* 5) ;; I/O error
(defconst *EBADF* 9) ;; Bad file number
(defconst *EEXIST* 17) ;; File exists
(defconst *ENOTDIR* 20) ;; Not a directory
(defconst *EISDIR* 21) ;; Is a directory
(defconst *ENOSPC* 28) ;; No space left on device
(defconst *ENAMETOOLONG* 36) ;; File name too long
(defund fat32-entry-p (x)
(declare (xargs :guard t))
(unsigned-byte-p 32 x))
(defund fat32-masked-entry-p (x)
(declare (xargs :guard t))
(unsigned-byte-p 28 x))
;; 0 is chosen as the default value based on this comment from Microsoft's FAT
;; overview:
;; The only time that the high 4 bits of FAT32 FAT entries should ever be
;; changed is when the volume is formatted, at which time the whole 32-bit FAT
;; entry should be zeroed, including the high 4 bits.
(defund fat32-entry-fix (x)
(declare (xargs :guard t))
(if (fat32-entry-p x)
x 0))
(defund fat32-masked-entry-fix (x)
(declare (xargs :guard t))
(if (fat32-masked-entry-p x)
x 0))
(in-theory (enable fat32-entry-p fat32-entry-fix fat32-masked-entry-p fat32-masked-entry-fix))
(defthm fat32-masked-entry-p-correctness-1
(implies (fat32-masked-entry-p x)
(natp x))
:rule-classes :forward-chaining)
;; Use a mask to take the low 28 bits.
(defund fat32-entry-mask (x)
(declare (xargs :guard (fat32-entry-p x)))
(logand x (- (ash 1 28) 1)))
(defthm
fat32-entry-mask-correctness-1
(fat32-masked-entry-p (fat32-entry-mask x))
:hints (("goal" :in-theory (e/d (fat32-entry-mask fat32-masked-entry-p)
(unsigned-byte-p logand-ash-lemma-1))
:use (:instance logand-ash-lemma-1 (c 28)
(i x)))))
(fty::deffixtype fat32-entry
:pred fat32-entry-p
:fix fat32-entry-fix
:equiv fat32-entry-equiv
:define t
:forward t
)
(fty::deffixtype fat32-masked-entry
:pred fat32-masked-entry-p
:fix fat32-masked-entry-fix
:equiv fat32-masked-entry-equiv
:define t
:forward t
)
(fty::deflist fat32-entry-list :elt-type fat32-entry-p :true-listp t)
(fty::deflist fat32-masked-entry-list :elt-type fat32-masked-entry-p :true-listp t)
(defthm nat-listp-if-fat32-masked-entry-list-p
(implies (fat32-masked-entry-list-p x)
(nat-listp x))
:rule-classes (:forward-chaining :rewrite))
(in-theory (disable fat32-entry-p fat32-entry-fix fat32-masked-entry-p fat32-masked-entry-fix))
(defthm member-of-fat32-entry-list
(implies (and (member-equal x lst)
(fat32-entry-list-p lst))
(fat32-entry-p x)))
(defthm
fat32-masked-entry-list-p-of-make-list-ac
(implies (and (fat32-masked-entry-p val)
(fat32-masked-entry-list-p ac))
(fat32-masked-entry-list-p (make-list-ac n val ac))))
(defthm fat32-masked-entry-list-p-of-append
(implies (true-listp x)
(equal (fat32-masked-entry-list-p (binary-append x y))
(and (fat32-masked-entry-list-p x)
(fat32-masked-entry-list-p y)))))
(defthm fat32-entry-list-p-of-update-nth
(implies (and (< key (len l))
(fat32-entry-list-p l))
(equal (fat32-entry-list-p (update-nth key val l))
(fat32-entry-p val))))
(defthm set-indices-in-fa-table-guard-lemma-2
(implies (fat32-entry-p x) (natp x))
:hints (("goal" :in-theory (enable fat32-entry-p)))
:rule-classes :forward-chaining)
(defthm fat32-entry-p-of-nth
(implies (and (fat32-entry-list-p l)
(< (nfix n) (len l)))
(fat32-entry-p (nth n l))))
(defund
fat32-update-lower-28
(entry masked-entry)
(declare
(xargs
:guard-hints
(("goal"
:in-theory (enable fat32-entry-p fat32-masked-entry-p)))
:guard (and (fat32-entry-p entry)
(fat32-masked-entry-p masked-entry))))
(logior (logand entry (- (ash 1 32) (ash 1 28)))
masked-entry))
(encapsulate
()
(local (include-book "ihs/logops-lemmas" :dir :system))
(defthm
fat32-update-lower-28-correctness-1
(implies
(and (fat32-entry-p entry)
(fat32-masked-entry-p masked-entry))
(fat32-entry-p (fat32-update-lower-28 entry masked-entry)))
:hints
(("goal"
:in-theory (e/d nil (unsigned-byte-p logand logior)
(fat32-entry-p fat32-masked-entry-p
fat32-update-lower-28)))
("goal''" :in-theory (enable unsigned-byte-p)))))
; :Redef helps here for overcoming lemmas that are incompatible here (and
; finding all such lemmas in the process).
(encapsulate
()
(local
(include-book "centaur/gl/gl" :dir :system))
(local
(def-gl-thm fat32-update-lower-28-correctness-2
:hyp (and (fat32-entry-p entry)
(fat32-masked-entry-p masked-entry))
:concl (equal (fat32-entry-mask (fat32-update-lower-28 entry
masked-entry))
masked-entry)
:g-bindings (gl::auto-bindings (:nat entry 33) (:nat masked-entry 29))))
(defthm
fat32-update-lower-28-correctness-2
(implies
(and (fat32-entry-p entry)
(fat32-masked-entry-p masked-entry))
(equal
(fat32-entry-mask (fat32-update-lower-28 entry masked-entry)) masked-entry))))
;; taken from page 18 of the fat overview - the constant 268435448 is written
;; out as 0xFFFFFF8 therein
(defund fat32-is-eof (fat-content)
(declare (xargs :guard (fat32-masked-entry-p fat-content)
:guard-hints (("Goal'" :in-theory (enable fat32-masked-entry-p)))))
(>= fat-content 268435448))
(defthm fat32-is-eof-correctness-1
(implies (< fat-content *ms-bad-cluster*)
(not (fat32-is-eof fat-content)))
:hints (("Goal" :in-theory (enable fat32-is-eof)) ))
(defun
fat32-build-index-list
(fa-table masked-current-cluster length cluster-size)
(declare
(xargs
:measure (nfix length)
:guard (and (fat32-entry-list-p fa-table)
(fat32-masked-entry-p masked-current-cluster)
(natp length)
(>= masked-current-cluster 2)
(< masked-current-cluster (len fa-table))
(integerp cluster-size)
(> cluster-size 0))))
(if
(or (zp length) (zp cluster-size))
;; This represents a problem case because masked-current-cluster is a
;; valid non-free cluster, but the length is 0. This loosely corresponds
;; to the infinite loop protection in the function
;; fs/fat/cache.c:fat_get_cluster
(mv nil (- *eio*))
(let
((masked-next-cluster
(fat32-entry-mask (nth masked-current-cluster fa-table))))
(if
(< masked-next-cluster 2)
(mv (list masked-current-cluster)
(- *eio*))
(if
(or (fat32-is-eof masked-next-cluster)
(>= masked-next-cluster (len fa-table)))
(mv (list masked-current-cluster) 0)
(b*
(((mv tail-index-list tail-error)
(fat32-build-index-list fa-table masked-next-cluster
(nfix (- length cluster-size))
cluster-size)))
(mv (list* masked-current-cluster tail-index-list)
tail-error)))))))
(defthm fat32-build-index-list-correctness-1
(implies (and (equal b (len fa-table))
(fat32-masked-entry-p masked-current-cluster)
(< masked-current-cluster (len fa-table)))
(b* (((mv index-list &)
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size)))
(bounded-nat-listp index-list b))))
(defthm
fat32-build-index-list-correctness-2
(implies
(and
(fat32-masked-entry-p masked-current-cluster)
(>= masked-current-cluster *ms-first-data-cluster*)
(< masked-current-cluster (len fa-table)))
(b* (((mv index-list &)
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size)))
(fat32-masked-entry-list-p index-list))))
(defthm
fat32-build-index-list-correctness-3
(b* (((mv & error-code)
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size)))
(and (integerp error-code)
(or (equal error-code 0)
(equal error-code (- *eio*))))))
(defthm
fat32-build-index-list-correctness-4
(implies
(fat32-masked-entry-p masked-current-cluster)
(mv-let
(index-list error-code)
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size)
(implies
(and (fat32-masked-entry-p key)
(< key (len fa-table))
(not (member-equal key index-list))
(equal error-code 0))
(equal
(fat32-build-index-list (update-nth key val fa-table)
masked-current-cluster
length cluster-size)
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size)))))
:hints
(("subgoal *1/3"
:expand
(fat32-build-index-list (update-nth key val fa-table)
masked-current-cluster
length cluster-size))))
(defthm
fat32-build-index-list-correctness-5
(implies
(and (fat32-masked-entry-p masked-current-cluster)
(<= *ms-first-data-cluster*
masked-current-cluster))
(lower-bounded-integer-listp
(mv-nth
0
(fat32-build-index-list fa-table masked-current-cluster
length cluster-size))
*ms-first-data-cluster*))
:hints
(("goal" :in-theory (enable lower-bounded-integer-listp))))
(defund
find-n-free-clusters-helper
(fa-table n start)
(declare (xargs :guard (and (fat32-entry-list-p fa-table)
(natp n)
(natp start))))
(if (or (atom fa-table) (zp n))
nil
(if (not (equal (fat32-entry-mask (car fa-table))
0))
(find-n-free-clusters-helper (cdr fa-table)
n (+ start 1))
(cons start
(find-n-free-clusters-helper (cdr fa-table)
(- n 1)
(+ start 1))))))
(defthmd
find-n-free-clusters-helper-correctness-1
(implies (and (fat32-entry-list-p fa-table)
(natp n)
(natp start)
(equal b (+ start (len fa-table))))
(bounded-nat-listp
(find-n-free-clusters-helper fa-table n start)
b))
:hints
(("goal'" :in-theory (enable find-n-free-clusters-helper))))
(defthmd
find-n-free-clusters-helper-correctness-2
(implies
(natp start)
(nat-listp (find-n-free-clusters-helper fa-table n start)))
:hints
(("goal" :in-theory (enable find-n-free-clusters-helper))))
(defthmd
find-n-free-clusters-helper-correctness-3
(implies
(and
(natp start)
(member-equal x (find-n-free-clusters-helper fa-table n start)))
(and (integerp x) (<= start x)))
:hints
(("goal" :in-theory (enable find-n-free-clusters-helper))))
(defthm
find-n-free-clusters-helper-correctness-4
(implies
(and (fat32-entry-list-p fa-table)
(natp n)
(natp start)
(member-equal
x
(find-n-free-clusters-helper fa-table n start)))
(equal (fat32-entry-mask (nth (- x start) fa-table))
0))
:hints
(("goal" :in-theory (enable find-n-free-clusters-helper)
:use find-n-free-clusters-helper-correctness-3)
("subgoal *1/2"
:use (:instance find-n-free-clusters-helper-correctness-3
(fa-table (cdr fa-table))
(start (+ 1 start))))))
(defthm find-n-free-clusters-guard-lemma-1
(implies (fat32-entry-list-p l)
(fat32-entry-list-p (nthcdr n l))))
(defund
find-n-free-clusters (fa-table n)
(declare (xargs :guard (and (fat32-entry-list-p fa-table)
(natp n))))
;; the first 2 clusters are excluded
(find-n-free-clusters-helper
(nthcdr *ms-first-data-cluster* fa-table)
n *ms-first-data-cluster*))
(defthm
find-n-free-clusters-correctness-1
(implies (and (fat32-entry-list-p fa-table)
(natp n)
(equal b (len fa-table))
(>= (len fa-table)
*ms-first-data-cluster*))
(bounded-nat-listp (find-n-free-clusters fa-table n)
b))
:hints
(("goal"
:in-theory (enable find-n-free-clusters)
:use
((:instance
find-n-free-clusters-helper-correctness-1
(start *ms-first-data-cluster*)
(fa-table (nthcdr *ms-first-data-cluster* fa-table))
(b (len fa-table))))))
:rule-classes
(:rewrite
(:linear
:corollary
(implies (and (fat32-entry-list-p fa-table)
(natp n)
(equal b (len fa-table))
(>= (len fa-table)
*ms-first-data-cluster*)
(consp (find-n-free-clusters fa-table n)))
(< (car (find-n-free-clusters fa-table n))
b)))))
(defthm
find-n-free-clusters-correctness-2
(nat-listp (find-n-free-clusters fa-table n))
:rule-classes
(:rewrite
(:rewrite
:corollary
(implies (consp (find-n-free-clusters fa-table n))
(and (nat-listp (cdr (find-n-free-clusters fa-table n)))
(integerp (car (find-n-free-clusters fa-table n))))))
(:linear
:corollary (implies (consp (find-n-free-clusters fa-table n))
(<= 0
(car (find-n-free-clusters fa-table n))))))
:hints
(("goal"
:in-theory (enable find-n-free-clusters
find-n-free-clusters-helper-correctness-2))))
(defthmd
find-n-free-clusters-correctness-3
(implies (member-equal x (find-n-free-clusters fa-table n))
(and (integerp x) (<= *ms-first-data-cluster* x)))
:hints
(("goal" :in-theory (enable find-n-free-clusters))
("goal'"
:use (:instance find-n-free-clusters-helper-correctness-3
(start *ms-first-data-cluster*)
(fa-table (nthcdr *ms-first-data-cluster* fa-table))))))
(defthmd
find-n-free-clusters-correctness-4
(implies
(and (fat32-entry-list-p fa-table)
(natp n)
(natp start)
(member-equal x (find-n-free-clusters fa-table n)))
(equal (fat32-entry-mask (nth x fa-table))
0))
:hints
(("goal"
:in-theory (enable find-n-free-clusters)
:use
(:instance
find-n-free-clusters-helper-correctness-4
(start *ms-first-data-cluster*)
(fa-table (nthcdr *ms-first-data-cluster* fa-table))))
("goal''"
:in-theory (disable member-of-a-nat-list)
:use
((:instance
member-of-a-nat-list
(lst (find-n-free-clusters-helper
(nthcdr *ms-first-data-cluster* fa-table)
n *ms-first-data-cluster*)))))
("subgoal 2"
:use
(:instance
find-n-free-clusters-helper-correctness-3
(fa-table (nthcdr *ms-first-data-cluster* fa-table))
(start *ms-first-data-cluster*)))))
(defthmd
fat32-masked-entry-list-p-alt
(equal (fat32-masked-entry-list-p x)
(bounded-nat-listp x *expt-2-28*))
:hints (("goal" :in-theory (enable fat32-masked-entry-p))))
(defthm
fat32-masked-entry-list-p-of-find-n-free-clusters
(implies (and (fat32-entry-list-p fa-table)
(natp n)
(>= (len fa-table) *ms-first-data-cluster*)
(<= (len fa-table) *ms-bad-cluster*))
(fat32-masked-entry-list-p (find-n-free-clusters fa-table n)))
:rule-classes
(:rewrite
(:rewrite
:corollary
(implies
(and (fat32-entry-list-p fa-table)
(natp n)
(>= (len fa-table) *ms-first-data-cluster*)
(<= (len fa-table) *ms-bad-cluster*))
(let ((l (find-n-free-clusters fa-table n)))
(implies (consp l)
(and (fat32-masked-entry-list-p (cdr l))
(fat32-masked-entry-p (car l))))))))
:hints (("goal" :in-theory (disable find-n-free-clusters-correctness-1)
:use ((:instance find-n-free-clusters-correctness-1
(b (len fa-table)))
(:instance fat32-masked-entry-list-p-alt
(x (find-n-free-clusters fa-table n)))
(:instance bounded-nat-listp-correctness-5
(l (find-n-free-clusters fa-table n))
(x (len fa-table))
(y *expt-2-28*))))))
(defthm
lower-bounded-integer-listp-of-find-n-free-clusters-helper
(implies (integerp start)
(lower-bounded-integer-listp
(find-n-free-clusters-helper fa-table n start)
start))
:hints
(("goal" :in-theory (enable lower-bounded-integer-listp
find-n-free-clusters-helper))
("subgoal *1/5.1'"
:use
(:instance lower-bounded-integer-listp-correctness-5
(l (find-n-free-clusters-helper (cdr fa-table)
(+ -1 n)
(+ 1 start)))
(x (+ 1 start))
(y start)))
("subgoal *1/3''"
:use
(:instance lower-bounded-integer-listp-correctness-5
(l (find-n-free-clusters-helper (cdr fa-table)
n (+ 1 start)))
(x (+ 1 start))
(y start)))))
(defthm
lower-bounded-integer-listp-of-find-n-free-clusters
(lower-bounded-integer-listp (find-n-free-clusters fa-table n)
*ms-first-data-cluster*)
:rule-classes
(:rewrite
(:rewrite
:corollary
(implies
(consp (find-n-free-clusters fa-table n))
(lower-bounded-integer-listp (cdr (find-n-free-clusters fa-table n))
*ms-first-data-cluster*))
:hints (("goal" :in-theory (enable lower-bounded-integer-listp))))
(:linear
:corollary (implies (consp (find-n-free-clusters fa-table n))
(<= *ms-first-data-cluster*
(car (find-n-free-clusters fa-table n))))
:hints (("goal" :in-theory (enable lower-bounded-integer-listp)))))
:hints (("goal" :in-theory (enable find-n-free-clusters))))
(defund
set-indices-in-fa-table
(fa-table index-list value-list)
(declare
(xargs :measure (acl2-count index-list)
:guard (and (fat32-entry-list-p fa-table)
(nat-listp index-list)
(fat32-masked-entry-list-p value-list)
(equal (len index-list)
(len value-list)))))
(if
(atom index-list)
fa-table
(let
((current-index (car index-list)))
(if
(or (not (natp current-index))
(>= current-index (len fa-table)))
fa-table
(set-indices-in-fa-table
(update-nth current-index
(fat32-update-lower-28 (nth current-index fa-table)
(car value-list))
fa-table)
(cdr index-list)
(cdr value-list))))))
(defthm
set-indices-in-fa-table-correctness-1
(implies
(and (fat32-entry-list-p fa-table)
(bounded-nat-listp index-list (len fa-table))
(fat32-masked-entry-list-p value-list)
(equal (len index-list)
(len value-list)))
(fat32-entry-list-p
(set-indices-in-fa-table fa-table index-list value-list)))
:hints (("Goal" :in-theory (enable set-indices-in-fa-table))))
(defthm
set-indices-in-fa-table-correctness-2
(equal (len (set-indices-in-fa-table fa-table index-list value-list))
(len fa-table))
:hints (("goal" :in-theory (enable set-indices-in-fa-table))))
;; Well, it might not be a great idea to borrow a numbering scheme from
;; set-indices.lisp
(defthm set-indices-in-fa-table-correctness-3
(implies (and (natp n)
(nat-listp index-list)
(not (member-equal n index-list)))
(equal (nth n (set-indices-in-fa-table fa-table index-list value-list))
(nth n fa-table)))
:hints (("Goal" :in-theory (enable set-indices-in-fa-table))))
(defthm set-indices-in-fa-table-correctness-4
(implies (and (natp key)
(< key (len l))
(nat-listp index-list)
(not (member-equal key index-list)))
(equal (set-indices-in-fa-table (update-nth key val l) index-list value-list)
(update-nth key val (set-indices-in-fa-table l index-list value-list))))
:hints (("Goal" :in-theory (enable set-indices-in-fa-table))))
|