--1-- SPDX-License-Identifier: BSD-2-Clause2--3-- Copyright (c) 2015 Pedro Souza <[email protected]>4-- Copyright (c) 2018 Kyle Evans <[email protected]>5-- All rights reserved.6--7-- Redistribution and use in source and binary forms, with or without8-- modification, are permitted provided that the following conditions9-- are met:10-- 1. Redistributions of source code must retain the above copyright11-- notice, this list of conditions and the following disclaimer.12-- 2. Redistributions in binary form must reproduce the above copyright13-- notice, this list of conditions and the following disclaimer in the14-- documentation and/or other materials provided with the distribution.15--16-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26-- SUCH DAMAGE.27--2829local core = require("core")30local screen = require("screen")3132local password = {}3334local INCORRECT_PASSWORD = "loader: incorrect password"35-- Asterisks as a password mask36local show_password_mask = false37local twiddle_chars = {"/", "-", "\\", "|"}38local screen_setup = false3940local function setup_screen()41screen.clear()42screen.defcursor()43screen_setup = true44end4546-- Module exports47function password.read(prompt_length)48local str = ""49local twiddle_pos = 15051local function draw_twiddle()52printc(twiddle_chars[twiddle_pos])53-- Reset cursor to just after the password prompt54screen.setcursor(prompt_length + 2, screen.default_y)55twiddle_pos = (twiddle_pos % #twiddle_chars) + 156end5758-- Space between the prompt and any on-screen feedback59printc(" ")60while true do61local ch = io.getchar()62if ch == core.KEY_ENTER then63break64end65if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then66if #str > 0 then67if show_password_mask then68printc("\008 \008")69else70draw_twiddle()71end72str = str:sub(1, #str - 1)73end74else75if show_password_mask then76printc("*")77else78draw_twiddle()79end80str = str .. string.char(ch)81end82end83return str84end8586function password.check()87-- pwd is optionally supplied if we want to check it88local function doPrompt(prompt, pwd)89local attempts = 19091local function clear_incorrect_text_prompt()92printc("\r" .. string.rep(" ", #INCORRECT_PASSWORD))93end9495if not screen_setup then96setup_screen()97end9899while true do100if attempts > 1 then101clear_incorrect_text_prompt()102end103screen.defcursor()104printc(prompt)105local read_pwd = password.read(#prompt)106if pwd == nil or pwd == read_pwd then107-- Clear the prompt + twiddle108printc(string.rep(" ", #prompt + 5))109return read_pwd110end111printc("\n" .. INCORRECT_PASSWORD)112attempts = attempts + 1113loader.delay(3*1000*1000)114end115end116local function compare(prompt, pwd)117if pwd == nil then118return119end120doPrompt(prompt, pwd)121end122123local boot_pwd = loader.getenv("bootlock_password")124compare("Bootlock password:", boot_pwd)125126local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")127if geli_prompt ~= nil and geli_prompt:lower() == "yes" then128local passphrase = doPrompt("GELI Passphrase:")129loader.setenv("kern.geom.eli.passphrase", passphrase)130end131132local pwd = loader.getenv("password")133if pwd ~= nil then134core.autoboot()135loader.setenv("autoboot_delay", "NO")136-- The autoboot sequence was interrupted, so we'll need to137-- prompt for a password. Put the screen back into a known138-- good state, otherwise we're drawing back a couple lines139-- in the middle of other text.140setup_screen()141end142compare("Loader password:", pwd)143end144145return password146147148