xref: /DragonOS/kernel/src/driver/video/fbdev/vesafb.rs (revision 52bcb59e9286def2b66d766f6bf6f46745795ec8)
1c566df45SLoGin use core::{
2c566df45SLoGin     ffi::{c_uint, c_void},
3c566df45SLoGin     mem::MaybeUninit,
4c566df45SLoGin     sync::atomic::AtomicBool,
5c566df45SLoGin };
6c566df45SLoGin 
7c566df45SLoGin use alloc::{
8c566df45SLoGin     string::{String, ToString},
9c566df45SLoGin     sync::{Arc, Weak},
10c566df45SLoGin     vec::Vec,
11c566df45SLoGin };
12c566df45SLoGin use system_error::SystemError;
13c566df45SLoGin use unified_init::macros::unified_init;
14c566df45SLoGin 
15c566df45SLoGin use crate::{
16c566df45SLoGin     arch::MMArch,
17c566df45SLoGin     driver::{
18c566df45SLoGin         base::{
19c566df45SLoGin             class::Class,
20c566df45SLoGin             device::{
21c566df45SLoGin                 bus::Bus, device_manager, driver::Driver, Device, DeviceState, DeviceType, IdTable,
22c566df45SLoGin             },
23c566df45SLoGin             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
24c566df45SLoGin             kset::KSet,
25c566df45SLoGin             platform::{
26c566df45SLoGin                 platform_device::{platform_device_manager, PlatformDevice},
27c566df45SLoGin                 platform_driver::{platform_driver_manager, PlatformDriver},
28c566df45SLoGin                 CompatibleTable,
29c566df45SLoGin             },
30c566df45SLoGin         },
3152da9a59SGnoCiYeH         serial::serial8250::send_to_default_serial8250_port,
3252da9a59SGnoCiYeH         video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET},
33c566df45SLoGin     },
34c566df45SLoGin     filesystem::{
35c566df45SLoGin         kernfs::KernFSInode,
36c566df45SLoGin         sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
37c566df45SLoGin         vfs::syscall::ModeType,
38c566df45SLoGin     },
39c566df45SLoGin     include::bindings::bindings::{
40c566df45SLoGin         multiboot2_get_Framebuffer_info, multiboot2_iter, multiboot_tag_framebuffer_info_t,
41c75ef4e2SLoGin         FRAME_BUFFER_MAPPING_OFFSET,
42c566df45SLoGin     },
43c566df45SLoGin     init::{boot_params, initcall::INITCALL_DEVICE},
44c566df45SLoGin     libs::{
45c566df45SLoGin         align::page_align_up,
46c566df45SLoGin         once::Once,
47c566df45SLoGin         rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
48c566df45SLoGin         spinlock::SpinLock,
49c566df45SLoGin     },
50c566df45SLoGin     mm::{
51c566df45SLoGin         allocator::page_frame::PageFrameCount, no_init::pseudo_map_phys, MemoryManagementArch,
52c566df45SLoGin         PhysAddr, VirtAddr,
53c566df45SLoGin     },
54c566df45SLoGin };
55c566df45SLoGin 
56c566df45SLoGin use super::base::{
57c566df45SLoGin     fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType,
58c566df45SLoGin     FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo,
5952da9a59SGnoCiYeH     FrameBufferInfoData, FrameBufferOps,
60c566df45SLoGin };
61c566df45SLoGin 
62c566df45SLoGin /// 当前机器上面是否有vesa帧缓冲区
63c566df45SLoGin static HAS_VESA_FB: AtomicBool = AtomicBool::new(false);
64c566df45SLoGin 
65c566df45SLoGin lazy_static! {
66c566df45SLoGin     static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo {
67c566df45SLoGin         id: FixedScreenInfo::name2id("VESA VGA"),
68c566df45SLoGin         fb_type: FbType::PackedPixels,
69c566df45SLoGin         accel: FbAccel::None,
70c566df45SLoGin         ..Default::default()
71c566df45SLoGin     });
72c566df45SLoGin     static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo {
73c566df45SLoGin         activate: FbActivateFlags::FB_ACTIVATE_NOW,
74c566df45SLoGin         height: None,
75c566df45SLoGin         width: None,
76c566df45SLoGin         right_margin: 32,
77c566df45SLoGin         upper_margin: 16,
78c566df45SLoGin         lower_margin: 4,
79c566df45SLoGin         vsync_len: 4,
80c566df45SLoGin         vmode: FbVModeFlags::FB_VMODE_NONINTERLACED,
81c566df45SLoGin 
82c566df45SLoGin         ..Default::default()
83c566df45SLoGin     });
84c566df45SLoGin }
85c566df45SLoGin 
86c566df45SLoGin #[derive(Debug)]
87c566df45SLoGin #[cast_to([sync] Device)]
88c566df45SLoGin #[cast_to([sync] PlatformDevice)]
89c566df45SLoGin pub struct VesaFb {
90c566df45SLoGin     inner: SpinLock<InnerVesaFb>,
91c566df45SLoGin     kobj_state: LockedKObjectState,
9252da9a59SGnoCiYeH     fb_data: RwLock<FrameBufferInfoData>,
93c566df45SLoGin }
94c566df45SLoGin 
95c566df45SLoGin impl VesaFb {
96c566df45SLoGin     pub const NAME: &'static str = "vesa_vga";
97c566df45SLoGin     pub fn new() -> Self {
9852da9a59SGnoCiYeH         let mut fb_info_data = FrameBufferInfoData::new();
9952da9a59SGnoCiYeH         fb_info_data.pesudo_palette.resize(256, 0);
100c566df45SLoGin         return Self {
101c566df45SLoGin             inner: SpinLock::new(InnerVesaFb {
102c566df45SLoGin                 bus: None,
103c566df45SLoGin                 class: None,
104c566df45SLoGin                 driver: None,
105c566df45SLoGin                 kern_inode: None,
106c566df45SLoGin                 parent: None,
107c566df45SLoGin                 kset: None,
108c566df45SLoGin                 kobj_type: None,
109c566df45SLoGin                 device_state: DeviceState::NotInitialized,
110c566df45SLoGin                 pdev_id: 0,
111c566df45SLoGin                 pdev_id_auto: false,
112c566df45SLoGin                 fb_id: FbId::INIT,
113c566df45SLoGin                 fb_device: None,
114c566df45SLoGin                 fb_state: FbState::Suspended,
115c566df45SLoGin             }),
116c566df45SLoGin             kobj_state: LockedKObjectState::new(None),
11752da9a59SGnoCiYeH             fb_data: RwLock::new(fb_info_data),
118c566df45SLoGin         };
119c566df45SLoGin     }
120c566df45SLoGin }
121c566df45SLoGin 
122c566df45SLoGin #[derive(Debug)]
123c566df45SLoGin struct InnerVesaFb {
124c566df45SLoGin     bus: Option<Weak<dyn Bus>>,
125c566df45SLoGin     class: Option<Arc<dyn Class>>,
126c566df45SLoGin     driver: Option<Weak<dyn Driver>>,
127c566df45SLoGin     kern_inode: Option<Arc<KernFSInode>>,
128c566df45SLoGin     parent: Option<Weak<dyn KObject>>,
129c566df45SLoGin     kset: Option<Arc<KSet>>,
130c566df45SLoGin     kobj_type: Option<&'static dyn KObjType>,
131c566df45SLoGin     device_state: DeviceState,
132c566df45SLoGin     pdev_id: i32,
133c566df45SLoGin     pdev_id_auto: bool,
134c566df45SLoGin     fb_id: FbId,
135c566df45SLoGin     fb_device: Option<Arc<FbDevice>>,
136c566df45SLoGin     fb_state: FbState,
137c566df45SLoGin }
138c566df45SLoGin 
139c566df45SLoGin impl FrameBuffer for VesaFb {
140c566df45SLoGin     fn fb_id(&self) -> FbId {
141c566df45SLoGin         self.inner.lock().fb_id
142c566df45SLoGin     }
143c566df45SLoGin 
144c566df45SLoGin     fn set_fb_id(&self, id: FbId) {
145c566df45SLoGin         self.inner.lock().fb_id = id;
146c566df45SLoGin     }
147c566df45SLoGin }
148c566df45SLoGin 
149c566df45SLoGin impl PlatformDevice for VesaFb {
150c566df45SLoGin     fn pdev_name(&self) -> &str {
151c566df45SLoGin         Self::NAME
152c566df45SLoGin     }
153c566df45SLoGin 
154c566df45SLoGin     fn set_pdev_id(&self, id: i32) {
155c566df45SLoGin         self.inner.lock().pdev_id = id;
156c566df45SLoGin     }
157c566df45SLoGin 
158c566df45SLoGin     fn set_pdev_id_auto(&self, id_auto: bool) {
159c566df45SLoGin         self.inner.lock().pdev_id_auto = id_auto;
160c566df45SLoGin     }
161c566df45SLoGin 
162c566df45SLoGin     fn compatible_table(&self) -> CompatibleTable {
163c566df45SLoGin         todo!()
164c566df45SLoGin     }
165c566df45SLoGin 
166c566df45SLoGin     fn is_initialized(&self) -> bool {
167c566df45SLoGin         self.inner.lock().device_state == DeviceState::Initialized
168c566df45SLoGin     }
169c566df45SLoGin 
170c566df45SLoGin     fn set_state(&self, set_state: DeviceState) {
171c566df45SLoGin         self.inner.lock().device_state = set_state;
172c566df45SLoGin     }
173c566df45SLoGin }
174c566df45SLoGin 
175c566df45SLoGin impl Device for VesaFb {
176c566df45SLoGin     fn dev_type(&self) -> DeviceType {
177c566df45SLoGin         DeviceType::Char
178c566df45SLoGin     }
179c566df45SLoGin 
180c566df45SLoGin     fn id_table(&self) -> IdTable {
181c566df45SLoGin         IdTable::new(self.name(), None)
182c566df45SLoGin     }
183c566df45SLoGin 
184c566df45SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
185c566df45SLoGin         self.inner.lock().bus.clone()
186c566df45SLoGin     }
187c566df45SLoGin 
188c566df45SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
189c566df45SLoGin         self.inner.lock().bus = bus;
190c566df45SLoGin     }
191c566df45SLoGin 
192c566df45SLoGin     fn set_class(&self, class: Option<Arc<dyn Class>>) {
193c566df45SLoGin         self.inner.lock().class = class;
194c566df45SLoGin     }
195c566df45SLoGin 
196c566df45SLoGin     fn driver(&self) -> Option<Arc<dyn Driver>> {
197c566df45SLoGin         self.inner.lock().driver.clone()?.upgrade()
198c566df45SLoGin     }
199c566df45SLoGin 
200c566df45SLoGin     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
201c566df45SLoGin         self.inner.lock().driver = driver;
202c566df45SLoGin     }
203c566df45SLoGin 
204c566df45SLoGin     fn is_dead(&self) -> bool {
205c566df45SLoGin         false
206c566df45SLoGin     }
207c566df45SLoGin 
208c566df45SLoGin     fn can_match(&self) -> bool {
209c566df45SLoGin         true
210c566df45SLoGin     }
211c566df45SLoGin 
212c566df45SLoGin     fn set_can_match(&self, _can_match: bool) {}
213c566df45SLoGin 
214c566df45SLoGin     fn state_synced(&self) -> bool {
215c566df45SLoGin         true
216c566df45SLoGin     }
217c566df45SLoGin }
218c566df45SLoGin 
219c566df45SLoGin impl KObject for VesaFb {
220c566df45SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
221c566df45SLoGin         self
222c566df45SLoGin     }
223c566df45SLoGin 
224c566df45SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
225c566df45SLoGin         self.inner.lock().kern_inode = inode;
226c566df45SLoGin     }
227c566df45SLoGin 
228c566df45SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
229c566df45SLoGin         self.inner.lock().kern_inode.clone()
230c566df45SLoGin     }
231c566df45SLoGin 
232c566df45SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
233c566df45SLoGin         self.inner.lock().parent.clone()
234c566df45SLoGin     }
235c566df45SLoGin 
236c566df45SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
237c566df45SLoGin         self.inner.lock().parent = parent;
238c566df45SLoGin     }
239c566df45SLoGin 
240c566df45SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
241c566df45SLoGin         self.inner.lock().kset.clone()
242c566df45SLoGin     }
243c566df45SLoGin 
244c566df45SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
245c566df45SLoGin         self.inner.lock().kset = kset;
246c566df45SLoGin     }
247c566df45SLoGin 
248c566df45SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
249c566df45SLoGin         self.inner.lock().kobj_type
250c566df45SLoGin     }
251c566df45SLoGin 
252c566df45SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
253c566df45SLoGin         self.inner.lock().kobj_type = ktype;
254c566df45SLoGin     }
255c566df45SLoGin 
256c566df45SLoGin     fn name(&self) -> String {
257c566df45SLoGin         Self::NAME.to_string()
258c566df45SLoGin     }
259c566df45SLoGin 
260c566df45SLoGin     fn set_name(&self, _name: String) {
261c566df45SLoGin         // do nothing
262c566df45SLoGin     }
263c566df45SLoGin 
264c566df45SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
265c566df45SLoGin         self.kobj_state.read()
266c566df45SLoGin     }
267c566df45SLoGin 
268c566df45SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
269c566df45SLoGin         self.kobj_state.write()
270c566df45SLoGin     }
271c566df45SLoGin 
272c566df45SLoGin     fn set_kobj_state(&self, state: KObjectState) {
273c566df45SLoGin         *self.kobj_state.write() = state;
274c566df45SLoGin     }
275c566df45SLoGin }
276c566df45SLoGin 
277c566df45SLoGin impl FrameBufferOps for VesaFb {
278c566df45SLoGin     fn fb_open(&self, _user: bool) {
279c566df45SLoGin         todo!()
280c566df45SLoGin     }
281c566df45SLoGin 
282c566df45SLoGin     fn fb_release(&self, _user: bool) {
283c566df45SLoGin         todo!()
284c566df45SLoGin     }
285c566df45SLoGin 
286c566df45SLoGin     fn fb_set_color_register(
287c566df45SLoGin         &self,
28852da9a59SGnoCiYeH         regno: u16,
28952da9a59SGnoCiYeH         mut red: u16,
29052da9a59SGnoCiYeH         mut green: u16,
29152da9a59SGnoCiYeH         mut blue: u16,
292c566df45SLoGin     ) -> Result<(), SystemError> {
29352da9a59SGnoCiYeH         let mut fb_data = self.framebuffer_info_data().write();
29452da9a59SGnoCiYeH         let var = self.current_fb_var();
29552da9a59SGnoCiYeH         if regno as usize >= fb_data.pesudo_palette.len() {
29652da9a59SGnoCiYeH             return Err(SystemError::E2BIG);
29752da9a59SGnoCiYeH         }
29852da9a59SGnoCiYeH 
29952da9a59SGnoCiYeH         if var.bits_per_pixel == 8 {
30052da9a59SGnoCiYeH             todo!("vesa_setpalette todo");
30152da9a59SGnoCiYeH         } else if regno < 16 {
30252da9a59SGnoCiYeH             match var.bits_per_pixel {
30352da9a59SGnoCiYeH                 16 => {
30452da9a59SGnoCiYeH                     if var.red.offset == 10 {
30552da9a59SGnoCiYeH                         // RGB 1:5:5:5
30652da9a59SGnoCiYeH                         fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1)
30752da9a59SGnoCiYeH                             | ((green as u32 & 0xf800) >> 6)
30852da9a59SGnoCiYeH                             | ((blue as u32 & 0xf800) >> 11);
30952da9a59SGnoCiYeH                     } else {
31052da9a59SGnoCiYeH                         fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800)
31152da9a59SGnoCiYeH                             | ((green as u32 & 0xfc00) >> 5)
31252da9a59SGnoCiYeH                             | ((blue as u32 & 0xf800) >> 11);
31352da9a59SGnoCiYeH                     }
31452da9a59SGnoCiYeH                 }
31552da9a59SGnoCiYeH                 24 | 32 => {
31652da9a59SGnoCiYeH                     red >>= 8;
31752da9a59SGnoCiYeH                     green >>= 8;
31852da9a59SGnoCiYeH                     blue >>= 8;
31952da9a59SGnoCiYeH                     fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset)
32052da9a59SGnoCiYeH                         | ((green as u32) << var.green.offset)
32152da9a59SGnoCiYeH                         | ((blue as u32) << var.blue.offset);
32252da9a59SGnoCiYeH                 }
32352da9a59SGnoCiYeH                 _ => {}
32452da9a59SGnoCiYeH             }
32552da9a59SGnoCiYeH         }
32652da9a59SGnoCiYeH 
32752da9a59SGnoCiYeH         Ok(())
328c566df45SLoGin     }
329c566df45SLoGin 
330c566df45SLoGin     fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> {
331c566df45SLoGin         todo!()
332c566df45SLoGin     }
333c566df45SLoGin 
334c566df45SLoGin     fn fb_destroy(&self) {
335c566df45SLoGin         todo!()
336c566df45SLoGin     }
33702343d0bSLoGin 
33802343d0bSLoGin     fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> {
33902343d0bSLoGin         let bp = boot_params().read();
34002343d0bSLoGin 
34102343d0bSLoGin         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
34202343d0bSLoGin         let size = self.current_fb_fix().smem_len;
34302343d0bSLoGin         drop(bp);
34402343d0bSLoGin         if pos >= size {
34502343d0bSLoGin             return Ok(0);
34602343d0bSLoGin         }
34702343d0bSLoGin 
34802343d0bSLoGin         let pos = pos as i64;
34902343d0bSLoGin         let size = size as i64;
35002343d0bSLoGin 
35102343d0bSLoGin         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
35202343d0bSLoGin 
35302343d0bSLoGin         let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) };
35402343d0bSLoGin         buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]);
35502343d0bSLoGin 
35602343d0bSLoGin         return Ok(len);
35702343d0bSLoGin     }
35802343d0bSLoGin 
35902343d0bSLoGin     fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> {
36002343d0bSLoGin         let bp = boot_params().read();
36102343d0bSLoGin 
36202343d0bSLoGin         let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
36302343d0bSLoGin         let size = self.current_fb_fix().smem_len;
36402343d0bSLoGin 
36502343d0bSLoGin         if pos >= size {
36602343d0bSLoGin             return Ok(0);
36702343d0bSLoGin         }
36802343d0bSLoGin 
36902343d0bSLoGin         let pos = pos as i64;
37002343d0bSLoGin         let size = size as i64;
37102343d0bSLoGin 
37202343d0bSLoGin         let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
37302343d0bSLoGin 
37402343d0bSLoGin         let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) };
37502343d0bSLoGin         slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]);
37602343d0bSLoGin 
37702343d0bSLoGin         return Ok(len);
37802343d0bSLoGin     }
37952da9a59SGnoCiYeH 
38052da9a59SGnoCiYeH     fn fb_image_blit(&self, image: &super::base::FbImage) {
38152da9a59SGnoCiYeH         self.generic_imageblit(image);
38252da9a59SGnoCiYeH     }
38352da9a59SGnoCiYeH 
38452da9a59SGnoCiYeH     /// ## 填充矩形
38552da9a59SGnoCiYeH     fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> {
38652da9a59SGnoCiYeH         // kwarn!("rect {rect:?}");
38752da9a59SGnoCiYeH 
38852da9a59SGnoCiYeH         let boot_param = boot_params().read();
38952da9a59SGnoCiYeH         let screen_base = boot_param
39052da9a59SGnoCiYeH             .screen_info
39152da9a59SGnoCiYeH             .lfb_virt_base
39252da9a59SGnoCiYeH             .ok_or(SystemError::ENODEV)?;
39352da9a59SGnoCiYeH         let fg;
39452da9a59SGnoCiYeH         if self.current_fb_fix().visual == FbVisual::TrueColor
39552da9a59SGnoCiYeH             || self.current_fb_fix().visual == FbVisual::DirectColor
39652da9a59SGnoCiYeH         {
39752da9a59SGnoCiYeH             fg = self.fb_data.read().pesudo_palette[rect.color as usize];
39852da9a59SGnoCiYeH         } else {
39952da9a59SGnoCiYeH             fg = rect.color;
40052da9a59SGnoCiYeH         }
40152da9a59SGnoCiYeH 
40252da9a59SGnoCiYeH         let bpp = self.current_fb_var().bits_per_pixel;
40352da9a59SGnoCiYeH         // 每行像素数
40452da9a59SGnoCiYeH         let line_offset = self.current_fb_var().xres;
40552da9a59SGnoCiYeH         match bpp {
40652da9a59SGnoCiYeH             32 => {
40752da9a59SGnoCiYeH                 let base = screen_base.as_ptr::<u32>();
40852da9a59SGnoCiYeH 
40952da9a59SGnoCiYeH                 for y in rect.dy..(rect.dy + rect.height) {
41052da9a59SGnoCiYeH                     for x in rect.dx..(rect.dx + rect.width) {
41152da9a59SGnoCiYeH                         unsafe { *base.add((y * line_offset + x) as usize) = fg };
41252da9a59SGnoCiYeH                     }
41352da9a59SGnoCiYeH                 }
41452da9a59SGnoCiYeH             }
41552da9a59SGnoCiYeH             _ => todo!(),
41652da9a59SGnoCiYeH         }
41752da9a59SGnoCiYeH 
41852da9a59SGnoCiYeH         Ok(())
41952da9a59SGnoCiYeH     }
42052da9a59SGnoCiYeH 
421*52bcb59eSGnoCiYeH     #[inline(never)]
422*52bcb59eSGnoCiYeH     fn fb_copyarea(&self, data: super::base::CopyAreaData) {
42352da9a59SGnoCiYeH         let bp = boot_params().read();
424*52bcb59eSGnoCiYeH         let base = bp.screen_info.lfb_virt_base.unwrap();
42552da9a59SGnoCiYeH         let var = self.current_fb_var();
42652da9a59SGnoCiYeH 
427*52bcb59eSGnoCiYeH         // 原区域或者目标区域全在屏幕外,则直接返回
428*52bcb59eSGnoCiYeH         if data.sx > var.xres as i32
429*52bcb59eSGnoCiYeH             || data.sy > var.yres as i32
430*52bcb59eSGnoCiYeH             || data.dx > var.xres as i32
431*52bcb59eSGnoCiYeH             || data.dy > var.yres as i32
432*52bcb59eSGnoCiYeH             || (data.sx + data.width as i32) < 0
433*52bcb59eSGnoCiYeH             || (data.sy + data.height as i32) < 0
434*52bcb59eSGnoCiYeH             || (data.dx + data.width as i32) < 0
435*52bcb59eSGnoCiYeH             || (data.dy + data.height as i32) < 0
43652da9a59SGnoCiYeH         {
437*52bcb59eSGnoCiYeH             return;
43852da9a59SGnoCiYeH         }
43952da9a59SGnoCiYeH 
440*52bcb59eSGnoCiYeH         // 求两个矩形可视范围交集
441*52bcb59eSGnoCiYeH         let (s_visiable_x, s_w) = if data.sx < 0 {
442*52bcb59eSGnoCiYeH             (0, (data.width - ((-data.sx) as u32)).min(var.xres))
443*52bcb59eSGnoCiYeH         } else {
444*52bcb59eSGnoCiYeH             let w = if data.sx as u32 + data.width > var.xres {
445*52bcb59eSGnoCiYeH                 var.xres - data.sx as u32
446*52bcb59eSGnoCiYeH             } else {
447*52bcb59eSGnoCiYeH                 data.width
448*52bcb59eSGnoCiYeH             };
449*52bcb59eSGnoCiYeH 
450*52bcb59eSGnoCiYeH             (data.sx, w)
451*52bcb59eSGnoCiYeH         };
452*52bcb59eSGnoCiYeH         let (s_visiable_y, s_h) = if data.sy < 0 {
453*52bcb59eSGnoCiYeH             (0, (data.height - ((-data.sy) as u32).min(var.yres)))
454*52bcb59eSGnoCiYeH         } else {
455*52bcb59eSGnoCiYeH             let h = if data.sy as u32 + data.height > var.yres {
456*52bcb59eSGnoCiYeH                 var.yres - data.sy as u32
457*52bcb59eSGnoCiYeH             } else {
458*52bcb59eSGnoCiYeH                 data.height
459*52bcb59eSGnoCiYeH             };
460*52bcb59eSGnoCiYeH 
461*52bcb59eSGnoCiYeH             (data.sy, h)
462*52bcb59eSGnoCiYeH         };
463*52bcb59eSGnoCiYeH 
464*52bcb59eSGnoCiYeH         let (d_visiable_x, d_w) = if data.dx < 0 {
465*52bcb59eSGnoCiYeH             (0, (data.width - ((-data.dx) as u32)).min(var.xres))
466*52bcb59eSGnoCiYeH         } else {
467*52bcb59eSGnoCiYeH             let w = if data.dx as u32 + data.width > var.xres {
468*52bcb59eSGnoCiYeH                 var.xres - data.dx as u32
469*52bcb59eSGnoCiYeH             } else {
470*52bcb59eSGnoCiYeH                 data.width
471*52bcb59eSGnoCiYeH             };
472*52bcb59eSGnoCiYeH 
473*52bcb59eSGnoCiYeH             (data.dx, w)
474*52bcb59eSGnoCiYeH         };
475*52bcb59eSGnoCiYeH         let (d_visiable_y, d_h) = if data.dy < 0 {
476*52bcb59eSGnoCiYeH             (0, (data.height - ((-data.dy) as u32).min(var.yres)))
477*52bcb59eSGnoCiYeH         } else {
478*52bcb59eSGnoCiYeH             let h = if data.dy as u32 + data.height > var.yres {
479*52bcb59eSGnoCiYeH                 var.yres - data.dy as u32
480*52bcb59eSGnoCiYeH             } else {
481*52bcb59eSGnoCiYeH                 data.height
482*52bcb59eSGnoCiYeH             };
483*52bcb59eSGnoCiYeH 
484*52bcb59eSGnoCiYeH             (data.dy, h)
485*52bcb59eSGnoCiYeH         };
486*52bcb59eSGnoCiYeH 
487*52bcb59eSGnoCiYeH         // 可视范围无交集
488*52bcb59eSGnoCiYeH         if !(d_h + s_h > data.height && s_w + d_w > data.width) {
489*52bcb59eSGnoCiYeH             return;
490*52bcb59eSGnoCiYeH         }
491*52bcb59eSGnoCiYeH 
492*52bcb59eSGnoCiYeH         // 可视区域左上角相对于矩形的坐标
493*52bcb59eSGnoCiYeH         let s_relative_x = s_visiable_x - data.sx;
494*52bcb59eSGnoCiYeH         let s_relative_y = s_visiable_y - data.sy;
495*52bcb59eSGnoCiYeH         let d_relative_x = d_visiable_x - data.dx;
496*52bcb59eSGnoCiYeH         let d_relative_y = d_visiable_y - data.dy;
497*52bcb59eSGnoCiYeH 
498*52bcb59eSGnoCiYeH         let visiable_x = s_relative_x.max(d_relative_x);
499*52bcb59eSGnoCiYeH         let visiable_y = s_relative_y.max(d_relative_y);
500*52bcb59eSGnoCiYeH         let visiable_h = d_h + s_h - data.height;
501*52bcb59eSGnoCiYeH         let visiable_w = d_w + s_w - data.width;
502*52bcb59eSGnoCiYeH 
503*52bcb59eSGnoCiYeH         let s_real_x = (visiable_x + data.sx) as u32;
504*52bcb59eSGnoCiYeH         let s_real_y = (visiable_y + data.sy) as u32;
505*52bcb59eSGnoCiYeH         let d_real_x = (visiable_x + data.dx) as u32;
506*52bcb59eSGnoCiYeH         let d_real_y = (visiable_y + data.dy) as u32;
507*52bcb59eSGnoCiYeH 
50852da9a59SGnoCiYeH         let bytes_per_pixel = var.bits_per_pixel >> 3;
50952da9a59SGnoCiYeH         let bytes_per_line = var.xres * bytes_per_pixel;
51052da9a59SGnoCiYeH 
511*52bcb59eSGnoCiYeH         let src =
512*52bcb59eSGnoCiYeH             base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize);
51352da9a59SGnoCiYeH 
514*52bcb59eSGnoCiYeH         let dst =
515*52bcb59eSGnoCiYeH             base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize);
51652da9a59SGnoCiYeH 
517*52bcb59eSGnoCiYeH         let size = (visiable_h * visiable_w) as usize;
51852da9a59SGnoCiYeH 
51952da9a59SGnoCiYeH         match bytes_per_pixel {
52052da9a59SGnoCiYeH             4 => {
52152da9a59SGnoCiYeH                 // 32bpp
52252da9a59SGnoCiYeH                 let mut dst = dst.as_ptr::<u32>();
52352da9a59SGnoCiYeH                 let mut src = src.as_ptr::<u32>();
524*52bcb59eSGnoCiYeH                 let line_offset = var.xres as usize;
52552da9a59SGnoCiYeH 
526*52bcb59eSGnoCiYeH                 if s_real_x > d_real_x {
527*52bcb59eSGnoCiYeH                     // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
52852da9a59SGnoCiYeH                     unsafe {
529*52bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
530*52bcb59eSGnoCiYeH                             core::ptr::copy(src, dst, visiable_w as usize);
531*52bcb59eSGnoCiYeH                             src = src.add(line_offset);
532*52bcb59eSGnoCiYeH                             dst = dst.add(visiable_w as usize);
53352da9a59SGnoCiYeH                         }
53452da9a59SGnoCiYeH                     }
53552da9a59SGnoCiYeH                 } else {
536*52bcb59eSGnoCiYeH                     let mut tmp: Vec<u32> = Vec::with_capacity(size);
537*52bcb59eSGnoCiYeH                     tmp.resize(size, 0);
538*52bcb59eSGnoCiYeH                     let mut tmp_ptr = tmp.as_mut_ptr();
539*52bcb59eSGnoCiYeH 
540*52bcb59eSGnoCiYeH                     // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
54152da9a59SGnoCiYeH                     unsafe {
542*52bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
543*52bcb59eSGnoCiYeH                             core::ptr::copy(src, tmp_ptr, visiable_w as usize);
544*52bcb59eSGnoCiYeH                             src = src.add(line_offset);
545*52bcb59eSGnoCiYeH                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
546*52bcb59eSGnoCiYeH                         }
547*52bcb59eSGnoCiYeH 
548*52bcb59eSGnoCiYeH                         tmp_ptr = tmp_ptr.sub(size);
549*52bcb59eSGnoCiYeH                         for _ in 0..visiable_h {
550*52bcb59eSGnoCiYeH                             core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
551*52bcb59eSGnoCiYeH                             dst = dst.add(line_offset);
552*52bcb59eSGnoCiYeH                             tmp_ptr = tmp_ptr.add(visiable_w as usize);
55352da9a59SGnoCiYeH                         }
55452da9a59SGnoCiYeH                     }
55552da9a59SGnoCiYeH                 }
55652da9a59SGnoCiYeH             }
55752da9a59SGnoCiYeH             _ => {
55852da9a59SGnoCiYeH                 todo!()
55952da9a59SGnoCiYeH             }
56052da9a59SGnoCiYeH         }
56152da9a59SGnoCiYeH     }
562c566df45SLoGin }
563c566df45SLoGin 
564c566df45SLoGin impl FrameBufferInfo for VesaFb {
565c566df45SLoGin     fn fb_device(&self) -> Option<Arc<FbDevice>> {
566c566df45SLoGin         self.inner.lock().fb_device.clone()
567c566df45SLoGin     }
568c566df45SLoGin 
569c566df45SLoGin     fn set_fb_device(&self, device: Option<Arc<FbDevice>>) {
570c566df45SLoGin         self.inner.lock().fb_device = device;
571c566df45SLoGin     }
572c566df45SLoGin 
573c566df45SLoGin     fn screen_size(&self) -> usize {
574c566df45SLoGin         todo!()
575c566df45SLoGin     }
576c566df45SLoGin 
577c566df45SLoGin     fn current_fb_var(&self) -> FbVarScreenInfo {
578c566df45SLoGin         VESAFB_DEFINED.read().clone()
579c566df45SLoGin     }
580c566df45SLoGin 
581c566df45SLoGin     fn current_fb_fix(&self) -> FixedScreenInfo {
582c566df45SLoGin         VESAFB_FIX_INFO.read().clone()
583c566df45SLoGin     }
584c566df45SLoGin 
585c566df45SLoGin     fn video_mode(&self) -> Option<&FbVideoMode> {
586c566df45SLoGin         todo!()
587c566df45SLoGin     }
588c566df45SLoGin 
589c566df45SLoGin     fn state(&self) -> FbState {
590c566df45SLoGin         self.inner.lock().fb_state
591c566df45SLoGin     }
59252da9a59SGnoCiYeH 
59352da9a59SGnoCiYeH     fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> {
59452da9a59SGnoCiYeH         &self.fb_data
59552da9a59SGnoCiYeH     }
596c566df45SLoGin }
597c566df45SLoGin 
598c566df45SLoGin #[derive(Debug)]
599c566df45SLoGin #[cast_to([sync] PlatformDriver)]
600c566df45SLoGin struct VesaFbDriver {
601c566df45SLoGin     inner: SpinLock<InnerVesaFbDriver>,
602c566df45SLoGin     kobj_state: LockedKObjectState,
603c566df45SLoGin }
604c566df45SLoGin 
605c566df45SLoGin impl VesaFbDriver {
606c566df45SLoGin     pub fn new() -> Arc<Self> {
607c566df45SLoGin         let r = Arc::new(Self {
608c566df45SLoGin             inner: SpinLock::new(InnerVesaFbDriver {
609c566df45SLoGin                 ktype: None,
610c566df45SLoGin                 kset: None,
611c566df45SLoGin                 parent: None,
612c566df45SLoGin                 kernfs_inode: None,
613c566df45SLoGin                 devices: Vec::new(),
614c566df45SLoGin                 bus: None,
615c566df45SLoGin                 self_ref: Weak::new(),
616c566df45SLoGin             }),
617c566df45SLoGin             kobj_state: LockedKObjectState::new(None),
618c566df45SLoGin         });
619c566df45SLoGin 
620c566df45SLoGin         r.inner.lock().self_ref = Arc::downgrade(&r);
621c566df45SLoGin 
622c566df45SLoGin         return r;
623c566df45SLoGin     }
624c566df45SLoGin }
625c566df45SLoGin 
626c566df45SLoGin #[derive(Debug)]
627c566df45SLoGin struct InnerVesaFbDriver {
628c566df45SLoGin     ktype: Option<&'static dyn KObjType>,
629c566df45SLoGin     kset: Option<Arc<KSet>>,
630c566df45SLoGin     parent: Option<Weak<dyn KObject>>,
631c566df45SLoGin     kernfs_inode: Option<Arc<KernFSInode>>,
632c566df45SLoGin     devices: Vec<Arc<dyn Device>>,
633c566df45SLoGin     bus: Option<Weak<dyn Bus>>,
634c566df45SLoGin 
635c566df45SLoGin     self_ref: Weak<VesaFbDriver>,
636c566df45SLoGin }
637c566df45SLoGin 
638c566df45SLoGin impl VesaFbDriver {
639c566df45SLoGin     const NAME: &'static str = "vesa-framebuffer";
640c566df45SLoGin }
641c566df45SLoGin 
642c566df45SLoGin impl PlatformDriver for VesaFbDriver {
643c566df45SLoGin     fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
644c566df45SLoGin         let device = device
645c566df45SLoGin             .clone()
646c566df45SLoGin             .arc_any()
647c566df45SLoGin             .downcast::<VesaFb>()
648c566df45SLoGin             .map_err(|_| SystemError::EINVAL)?;
649c566df45SLoGin 
650c566df45SLoGin         device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone()));
651c566df45SLoGin 
652c566df45SLoGin         return Ok(());
653c566df45SLoGin     }
654c566df45SLoGin 
655c566df45SLoGin     fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
656c566df45SLoGin         todo!()
657c566df45SLoGin     }
658c566df45SLoGin 
659c566df45SLoGin     fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
660c566df45SLoGin         // do nothing
661c566df45SLoGin         return Ok(());
662c566df45SLoGin     }
663c566df45SLoGin 
664c566df45SLoGin     fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
665c566df45SLoGin         // do nothing
666c566df45SLoGin         return Ok(());
667c566df45SLoGin     }
668c566df45SLoGin 
669c566df45SLoGin     fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
670c566df45SLoGin         todo!()
671c566df45SLoGin     }
672c566df45SLoGin }
673c566df45SLoGin 
674c566df45SLoGin impl Driver for VesaFbDriver {
675c566df45SLoGin     fn id_table(&self) -> Option<IdTable> {
676c566df45SLoGin         Some(IdTable::new(VesaFb::NAME.to_string(), None))
677c566df45SLoGin     }
678c566df45SLoGin 
679c566df45SLoGin     fn devices(&self) -> Vec<Arc<dyn Device>> {
680c566df45SLoGin         self.inner.lock().devices.clone()
681c566df45SLoGin     }
682c566df45SLoGin 
683c566df45SLoGin     fn add_device(&self, device: Arc<dyn Device>) {
684c566df45SLoGin         let mut guard = self.inner.lock();
685c566df45SLoGin         // check if the device is already in the list
686c566df45SLoGin         if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
687c566df45SLoGin             return;
688c566df45SLoGin         }
689c566df45SLoGin 
690c566df45SLoGin         guard.devices.push(device);
691c566df45SLoGin     }
692c566df45SLoGin 
693c566df45SLoGin     fn delete_device(&self, device: &Arc<dyn Device>) {
694c566df45SLoGin         let mut guard = self.inner.lock();
695c566df45SLoGin         guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
696c566df45SLoGin     }
697c566df45SLoGin 
698c566df45SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
699c566df45SLoGin         self.inner.lock().bus = bus;
700c566df45SLoGin     }
701c566df45SLoGin 
702c566df45SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
703c566df45SLoGin         self.inner.lock().bus.clone()
704c566df45SLoGin     }
705c566df45SLoGin 
706c566df45SLoGin     fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] {
707c566df45SLoGin         return &[&VesaFbAnonAttributeGroup];
708c566df45SLoGin     }
709c566df45SLoGin }
710c566df45SLoGin 
711c566df45SLoGin impl KObject for VesaFbDriver {
712c566df45SLoGin     fn as_any_ref(&self) -> &dyn core::any::Any {
713c566df45SLoGin         self
714c566df45SLoGin     }
715c566df45SLoGin 
716c566df45SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
717c566df45SLoGin         self.inner.lock().kernfs_inode = inode;
718c566df45SLoGin     }
719c566df45SLoGin 
720c566df45SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
721c566df45SLoGin         self.inner.lock().kernfs_inode.clone()
722c566df45SLoGin     }
723c566df45SLoGin 
724c566df45SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
725c566df45SLoGin         self.inner.lock().parent.clone()
726c566df45SLoGin     }
727c566df45SLoGin 
728c566df45SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
729c566df45SLoGin         self.inner.lock().parent = parent;
730c566df45SLoGin     }
731c566df45SLoGin 
732c566df45SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
733c566df45SLoGin         self.inner.lock().kset.clone()
734c566df45SLoGin     }
735c566df45SLoGin 
736c566df45SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
737c566df45SLoGin         self.inner.lock().kset = kset;
738c566df45SLoGin     }
739c566df45SLoGin 
740c566df45SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
741c566df45SLoGin         self.inner.lock().ktype
742c566df45SLoGin     }
743c566df45SLoGin 
744c566df45SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
745c566df45SLoGin         self.inner.lock().ktype = ktype;
746c566df45SLoGin     }
747c566df45SLoGin 
748c566df45SLoGin     fn name(&self) -> String {
749c566df45SLoGin         Self::NAME.to_string()
750c566df45SLoGin     }
751c566df45SLoGin 
752c566df45SLoGin     fn set_name(&self, _name: String) {
753c566df45SLoGin         // do nothing
754c566df45SLoGin     }
755c566df45SLoGin 
756c566df45SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
757c566df45SLoGin         self.kobj_state.read()
758c566df45SLoGin     }
759c566df45SLoGin 
760c566df45SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
761c566df45SLoGin         self.kobj_state.write()
762c566df45SLoGin     }
763c566df45SLoGin 
764c566df45SLoGin     fn set_kobj_state(&self, state: KObjectState) {
765c566df45SLoGin         *self.kobj_state.write() = state;
766c566df45SLoGin     }
767c566df45SLoGin }
768c566df45SLoGin 
769c566df45SLoGin #[derive(Debug)]
770c566df45SLoGin struct VesaFbAnonAttributeGroup;
771c566df45SLoGin 
772c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup {
773c566df45SLoGin     fn name(&self) -> Option<&str> {
774c566df45SLoGin         None
775c566df45SLoGin     }
776c566df45SLoGin 
777c566df45SLoGin     fn attrs(&self) -> &[&'static dyn Attribute] {
778c566df45SLoGin         &[&AnonAttrPhysAddr as &'static dyn Attribute]
779c566df45SLoGin     }
780c566df45SLoGin 
781c566df45SLoGin     fn is_visible(
782c566df45SLoGin         &self,
783c566df45SLoGin         _kobj: Arc<dyn KObject>,
784c566df45SLoGin         attr: &'static dyn Attribute,
785c566df45SLoGin     ) -> Option<ModeType> {
786c566df45SLoGin         Some(attr.mode())
787c566df45SLoGin     }
788c566df45SLoGin }
789c566df45SLoGin 
790c566df45SLoGin #[derive(Debug)]
791c566df45SLoGin struct AnonAttrPhysAddr;
792c566df45SLoGin 
793c566df45SLoGin impl Attribute for AnonAttrPhysAddr {
794c566df45SLoGin     fn name(&self) -> &str {
795c566df45SLoGin         "smem_start"
796c566df45SLoGin     }
797c566df45SLoGin 
798c566df45SLoGin     fn mode(&self) -> ModeType {
799c566df45SLoGin         ModeType::S_IRUGO
800c566df45SLoGin     }
801c566df45SLoGin 
802c566df45SLoGin     fn support(&self) -> SysFSOpsSupport {
803196b75dcSLoGin         SysFSOpsSupport::ATTR_SHOW
804c566df45SLoGin     }
805c566df45SLoGin 
806c566df45SLoGin     fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
807c566df45SLoGin         sysfs_emit_str(
808c566df45SLoGin             buf,
809c566df45SLoGin             format!(
810c566df45SLoGin                 "0x{:x}\n",
811c566df45SLoGin                 VESAFB_FIX_INFO
812c566df45SLoGin                     .read()
813c566df45SLoGin                     .smem_start
814c566df45SLoGin                     .unwrap_or(PhysAddr::new(0))
815c566df45SLoGin                     .data()
816c566df45SLoGin             )
817c566df45SLoGin             .as_str(),
818c566df45SLoGin         )
819c566df45SLoGin     }
820c566df45SLoGin }
821c566df45SLoGin 
822c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
823c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> {
824c566df45SLoGin     let driver = VesaFbDriver::new();
825c566df45SLoGin 
826c566df45SLoGin     platform_driver_manager().register(driver)?;
827c566df45SLoGin 
828c566df45SLoGin     return Ok(());
829c566df45SLoGin }
830c566df45SLoGin 
831c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb
832c566df45SLoGin pub fn vesafb_early_init() -> Result<VirtAddr, SystemError> {
833c566df45SLoGin     let mut _reserved: u32 = 0;
834c566df45SLoGin 
835c566df45SLoGin     let mut fb_info: MaybeUninit<multiboot_tag_framebuffer_info_t> = MaybeUninit::uninit();
836c566df45SLoGin     //从multiboot2中读取帧缓冲区信息至fb_info
837c566df45SLoGin 
838c566df45SLoGin     // todo: 换成rust的,并且检测是否成功获取
839c566df45SLoGin     unsafe {
840c566df45SLoGin         multiboot2_iter(
841c566df45SLoGin             Some(multiboot2_get_Framebuffer_info),
842c566df45SLoGin             fb_info.as_mut_ptr() as usize as *mut c_void,
843c566df45SLoGin             &mut _reserved as *mut c_uint,
844c566df45SLoGin         )
845c566df45SLoGin     };
846c566df45SLoGin     unsafe { fb_info.assume_init() };
847c566df45SLoGin     let fb_info: multiboot_tag_framebuffer_info_t = unsafe { core::mem::transmute(fb_info) };
848c566df45SLoGin 
849c566df45SLoGin     // todo: 判断是否有vesa帧缓冲区,这里暂时直接设置true
850c566df45SLoGin     HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst);
851c566df45SLoGin 
852c566df45SLoGin     let width = fb_info.framebuffer_width;
853c566df45SLoGin     let height = fb_info.framebuffer_height;
854c566df45SLoGin 
855c566df45SLoGin     let mut boot_params_guard = boot_params().write();
856c566df45SLoGin     let boottime_screen_info = &mut boot_params_guard.screen_info;
857c566df45SLoGin 
858c566df45SLoGin     boottime_screen_info.is_vga = true;
859c566df45SLoGin 
860c566df45SLoGin     boottime_screen_info.lfb_base = PhysAddr::new(fb_info.framebuffer_addr as usize);
861c566df45SLoGin 
862c566df45SLoGin     if fb_info.framebuffer_type == 2 {
863c566df45SLoGin         //当type=2时,width与height用字符数表示,故depth=8
864c566df45SLoGin         boottime_screen_info.origin_video_cols = width as u8;
865c566df45SLoGin         boottime_screen_info.origin_video_lines = height as u8;
866c566df45SLoGin         boottime_screen_info.video_type = BootTimeVideoType::Mda;
867c566df45SLoGin         boottime_screen_info.lfb_depth = 8;
868c566df45SLoGin     } else {
869c566df45SLoGin         //否则为图像模式,depth应参照帧缓冲区信息里面的每个像素的位数
870c566df45SLoGin         boottime_screen_info.lfb_width = width;
871c566df45SLoGin         boottime_screen_info.lfb_height = height;
872c566df45SLoGin         boottime_screen_info.video_type = BootTimeVideoType::Vlfb;
873c566df45SLoGin         boottime_screen_info.lfb_depth = fb_info.framebuffer_bpp as u8;
874c566df45SLoGin     }
875c566df45SLoGin 
876c566df45SLoGin     boottime_screen_info.lfb_size =
877c566df45SLoGin         (width * height * ((fb_info.framebuffer_bpp as u32 + 7) / 8)) as usize;
878c566df45SLoGin 
879c75ef4e2SLoGin     // let buf_vaddr = VirtAddr::new(0xffff800003200000);
880c75ef4e2SLoGin     let buf_vaddr = VirtAddr::new(
881c75ef4e2SLoGin         crate::include::bindings::bindings::SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE as usize
882c75ef4e2SLoGin             + FRAME_BUFFER_MAPPING_OFFSET as usize,
883c75ef4e2SLoGin     );
884c566df45SLoGin     boottime_screen_info.lfb_virt_base = Some(buf_vaddr);
885c566df45SLoGin 
886c566df45SLoGin     let init_text = "Video driver to map.\n\0";
887c566df45SLoGin     send_to_default_serial8250_port(init_text.as_bytes());
888c566df45SLoGin 
889c566df45SLoGin     // 地址映射
890c566df45SLoGin     let paddr = PhysAddr::new(fb_info.framebuffer_addr as usize);
891c566df45SLoGin     let count =
892c566df45SLoGin         PageFrameCount::new(page_align_up(boottime_screen_info.lfb_size) / MMArch::PAGE_SIZE);
893c566df45SLoGin     unsafe { pseudo_map_phys(buf_vaddr, paddr, count) };
894c566df45SLoGin     return Ok(buf_vaddr);
895c566df45SLoGin }
896c566df45SLoGin 
897c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
898c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> {
899c566df45SLoGin     // 如果没有vesa帧缓冲区,直接返回
900c566df45SLoGin     if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) {
901c566df45SLoGin         return Ok(());
902c566df45SLoGin     }
903c566df45SLoGin 
904c566df45SLoGin     static INIT: Once = Once::new();
905c566df45SLoGin     INIT.call_once(|| {
906c566df45SLoGin         kinfo!("vesa fb device init");
90752da9a59SGnoCiYeH         let device = Arc::new(VesaFb::new());
908c566df45SLoGin 
90952da9a59SGnoCiYeH         let mut fb_fix = VESAFB_FIX_INFO.write_irqsave();
91052da9a59SGnoCiYeH         let mut fb_var = VESAFB_DEFINED.write_irqsave();
911c566df45SLoGin 
912c566df45SLoGin         let boot_params_guard = boot_params().read();
913c566df45SLoGin         let boottime_screen_info = &boot_params_guard.screen_info;
914c566df45SLoGin 
91552da9a59SGnoCiYeH         fb_fix.smem_start = Some(boottime_screen_info.lfb_base);
91652da9a59SGnoCiYeH         fb_fix.smem_len = boottime_screen_info.lfb_size;
917c566df45SLoGin 
918c566df45SLoGin         if boottime_screen_info.video_type == BootTimeVideoType::Mda {
91952da9a59SGnoCiYeH             fb_fix.visual = FbVisual::Mono10;
92052da9a59SGnoCiYeH             fb_var.bits_per_pixel = 8;
92152da9a59SGnoCiYeH             fb_fix.line_length =
92252da9a59SGnoCiYeH                 (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8);
92352da9a59SGnoCiYeH             fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32;
92452da9a59SGnoCiYeH             fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32;
925c566df45SLoGin         } else {
92652da9a59SGnoCiYeH             fb_fix.visual = FbVisual::TrueColor;
92752da9a59SGnoCiYeH             fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32;
92852da9a59SGnoCiYeH             fb_fix.line_length =
92952da9a59SGnoCiYeH                 (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8);
93052da9a59SGnoCiYeH             fb_var.xres_virtual = boottime_screen_info.lfb_width as u32;
93152da9a59SGnoCiYeH             fb_var.yres_virtual = boottime_screen_info.lfb_height as u32;
93252da9a59SGnoCiYeH             fb_var.xres = boottime_screen_info.lfb_width as u32;
93352da9a59SGnoCiYeH             fb_var.yres = boottime_screen_info.lfb_height as u32;
934c566df45SLoGin         }
935c566df45SLoGin 
93652da9a59SGnoCiYeH         fb_var.red.length = boottime_screen_info.red_size as u32;
93752da9a59SGnoCiYeH         fb_var.green.length = boottime_screen_info.green_size as u32;
93852da9a59SGnoCiYeH         fb_var.blue.length = boottime_screen_info.blue_size as u32;
939c566df45SLoGin 
94052da9a59SGnoCiYeH         fb_var.red.offset = boottime_screen_info.red_pos as u32;
94152da9a59SGnoCiYeH         fb_var.green.offset = boottime_screen_info.green_pos as u32;
94252da9a59SGnoCiYeH         fb_var.blue.offset = boottime_screen_info.blue_pos as u32;
94352da9a59SGnoCiYeH 
94452da9a59SGnoCiYeH         // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面
94552da9a59SGnoCiYeH         fb_var.red.offset = 16;
94652da9a59SGnoCiYeH         fb_var.green.offset = 8;
94752da9a59SGnoCiYeH         fb_var.blue.offset = 0;
94852da9a59SGnoCiYeH 
94952da9a59SGnoCiYeH         if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 {
95052da9a59SGnoCiYeH             fb_var.red.length = fb_var.bits_per_pixel;
95152da9a59SGnoCiYeH             fb_var.green.length = fb_var.bits_per_pixel;
95252da9a59SGnoCiYeH             fb_var.blue.length = fb_var.bits_per_pixel;
95352da9a59SGnoCiYeH         }
95452da9a59SGnoCiYeH 
955c566df45SLoGin         device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>));
956c566df45SLoGin 
957c566df45SLoGin         platform_device_manager()
958c566df45SLoGin             .device_add(device.clone() as Arc<dyn PlatformDevice>)
959c566df45SLoGin             .expect("vesa_fb_device_init: platform_device_manager().device_add failed");
960c566df45SLoGin 
961c566df45SLoGin         frame_buffer_manager()
962c566df45SLoGin             .register_fb(device.clone() as Arc<dyn FrameBuffer>)
963c566df45SLoGin             .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed");
964c566df45SLoGin 
96552da9a59SGnoCiYeH         // 加入全局fb表
96652da9a59SGnoCiYeH         let mut guard = FRAME_BUFFER_SET.write();
96752da9a59SGnoCiYeH         if guard.get(device.fb_id().data() as usize).unwrap().is_some() {
96852da9a59SGnoCiYeH             kwarn!(
96952da9a59SGnoCiYeH                 "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET",
97052da9a59SGnoCiYeH                 device.fb_id()
97152da9a59SGnoCiYeH             );
97252da9a59SGnoCiYeH         }
97352da9a59SGnoCiYeH         guard[device.fb_id().data() as usize] = Some(device.clone());
97452da9a59SGnoCiYeH 
975c566df45SLoGin         // 设置vesa fb的状态为运行中
976c566df45SLoGin         device.inner.lock().fb_state = FbState::Running;
977c566df45SLoGin     });
978c566df45SLoGin 
979c566df45SLoGin     return Ok(());
980c566df45SLoGin }
981